首页 > 代码库 > python-装饰器,类与对象,私有字段,析构,__call__,继承,多继承,接口
python-装饰器,类与对象,私有字段,析构,__call__,继承,多继承,接口
1、装饰器执行流程
装饰器:将原函数替换为wrapper函数
def outer()
@outer --- func1作为参数传入outer()
def wrapper() --- wrapper()放入内存
return wrapper --- 返回wrapper地址,Func1重新赋值为wrapper
Func1() --- 调用func1函数
def wrapper()
print ‘验证‘
def Func1()
print ‘func1‘
[root@localhost decorate]# python main.py
验证
func1
验证
func2
[root@localhost decorate]# cat main.py
#!/usr/bin/python27
#coding:utf-8
def outer(fun):
def wrapper():
print ‘验证‘
fun()
return wrapper
@outer
def Func1():
print ‘func1‘
@outer
def Func2():
print ‘func2‘
‘‘‘
Func1=
def wrapper():
print ‘验证‘
fun()
‘‘‘
Func1()
Func2()
[root@localhost decorate]#
带参数的装饰器
[root@localhost decorate]# python27 main1.py
验证
func1 alex
[root@localhost decorate]# cat main1.py
#!/usr/bin/python27
#coding:utf-8
def outer(fun):
def wrapper(arg):
print ‘验证‘
fun(arg)
return wrapper
@outer
def Func1(arg):
print ‘func1‘,arg
‘‘‘
Func1=
def wrapper(arg):
print ‘验证‘
fun(arg)
‘‘‘
Func1(‘alex‘)
[root@localhost decorate]#
在函数执行的前后,执行任意函数
def Filter(before_func,after_func):
def outer(main_func):
def wrapper(request,kargs):
before_result = before_func(request,kargs)
if(before_result != None):
return before_result;
main_result = main_func(request,kargs)
if(main_result != None):
return main_result;
after_result = after_func(request,kargs)
if(after_result != None):
return after_result;
return wrapper
return outer
@Filter(AccountFilter.Before, AccountFilter.After) def List(request,kargs):
pass
2、类与对象
封装性:将name,age属性封装到self中
[root@localhost ~]# python27 index.py
cxiong 29
xmzhang 28
中国
[root@localhost ~]# cat index.py
#!/usr/bin/python27
#coding:utf-8
class Person():
nation=‘中国‘ --- 静态字段:nation属于class
def __init__(self,name,age):
self.name=name --- 动态字段:self.name属于对象
self.age=age ---动态字段:self.age属于对象
p1=Person(‘cxiong‘,29)
p2=Person(‘xmzhang‘,28)
print p1.name,p1.age
print p2.name,p2.age
print p1.nation
[root@localhost ~]#
注意:类不能访问动态字段;对象可以访问静态字段
静态字段、动态字段、静态方法、动态方法和装饰器
作用:提供统一的方法和数据,用于处理类的请求
[root@localhost ~]# cat index.py
#!/usr/bin/python27
#coding:utf-8
class Person():
#静态字段
nation=‘中国‘
def __init__(self,name,age):
#动态字段
self.name=name
self.age=age
#动态方法
def Eat(self):
print self.name+‘ eating...‘
#静态方法 --- 不需要实例化类,即可调用方法
@staticmethod
def Breath():
print ‘breathing...‘
#装饰器 --- 将方法访问形式作为字段形式访问
@property
def Sing(self):
print self.name+‘ singing...‘
p1=Person(‘cxiong‘,29)
p2=Person(‘xmzhang‘,28)
print p1.name,p1.age
print p2.name,p2.age
print p1.nation
p1.Eat()
p2.Eat()
Person.Breath()
p1.Sing
3、面向对象与函数式编程的区别
静态方法与模块形式的区别:内存上没区别,区别在于,静态方法逻辑上属于类;
静态方法是面向对象语言解决对象重复构造方法时产生的,python模块化语言也可以解决;
多态:将相似的方法包装在同一个模块中;
python支持模块化编程,也支持反射;等同于面向对象编程java、.net
面向对象:可以创建模板
4、私有字段和私有方法
作用:安全
[root@localhost ~]# python index1.py
male
[root@localhost ~]# cat index1.py
#!/usr/bin/python27
#coding:utf-8
class Person():
#静态字段
nation=‘中国‘
def __init__(self,name,age,gender):
#动态字段
self.name=name
self.age=age
#私有字段
self.__gender=gender
#动态方法
def Eat(self):
print self.name+‘ eating...‘
#静态方法
@staticmethod
def Breath():
print ‘breathing...‘
#装饰器
@property
def Sing(self):
print self.name+‘ singing...‘
@property --- 私有字段不能被外部访问,但是可以使用方法访问
def ShowGender(self):
return self.__gender
p1=Person(‘cxiong‘,29,‘male‘)
p2=Person(‘xmzhang‘,28,‘female‘)
print p1.ShowGender
只读私有字段和可改写私有字段
[root@localhost ~]# cat index1.py
#!/usr/bin/python27
#coding:utf-8
class Person(): --- 没有继承object是可读可写;继承了object一个是可读,一个是可写
#静态字段
nation=‘中国‘
def __init__(self,name,age,gender): --- 构造函数
#动态字段
self.name=name
self.age=age
#私有字段
self.__gender=gender
#动态方法
def Eat(self):
print self.name+‘ eating...‘
#静态方法
@staticmethod
def Breath():
print ‘breathing...‘
#装饰器
@property
def Sing(self):
print self.name+‘ singing...‘
#只读私有字段
@property
def ShowGender(self):
return self.__gender
#可改私有字段
@ShowGender.setter
def ShowGender(self,value):
self.__gender=value
p1=Person(‘cxiong‘,29,‘male‘)
p2=Person(‘xmzhang‘,28,‘female‘)
print p1.ShowGender
p1.ShowGender=‘female‘
print p1.ShowGender
5、析构函数及__call__方法
[root@localhost ~]# python index1.py
male
female
解释器要销毁person了...
解释器要销毁person了...
[root@localhost ~]# cat index1.py
#!/usr/bin/python27
#coding:utf-8
class Person(object):
#静态字段
nation=‘中国‘
#构造函数
def __init__(self,name,age,gender):
#动态字段
self.name=name
self.age=age
#私有字段
self.__gender=gender
#析构函数,用于销毁对象时使用;一般不使用,常用于操作文件
def __del__(self):
print ‘解释器要销毁person了...‘
def __call__(self):
print ‘call‘
#动态方法
def Eat(self):
print self.name+‘ eating...‘
#静态方法
@staticmethod
def Breath():
print ‘breathing...‘
#装饰器
@property
def Sing(self):
print self.name+‘ singing...‘
#只读私有字段
@property
def ShowGender(self):
return self.__gender
#可改私有字段
@ShowGender.setter
def ShowGender(self,value):
self.__gender=value
p1=Person(‘cxiong‘,29,‘male‘)
p2=Person(‘xmzhang‘,28,‘female‘)
print p1.ShowGender
p1.ShowGender=‘female‘
print p1.ShowGender
p1() #执行__call__方法
[root@localhost ~]# python index1.py
male
female
call
解释器要销毁person了...
解释器要销毁person了...
6、继承
[root@localhost ~]# python index1.py
work hard...
cxiong singing...
breathing...
解释器要销毁person了...
[root@localhost ~]# cat index1.py
#!/usr/bin/python27
#coding:utf-8
class Person(object):
#静态字段
nation=‘中国‘
#构造函数
def __init__(self,name,age,gender):
#动态字段
self.name=name
self.age=age
#私有字段
self.__gender=gender
#析构函数
def __del__(self):
print ‘解释器要销毁person了...‘
#动态方法
def Eat(self):
print self.name+‘ eating...‘
#
def __call__(self):
print ‘call‘
#静态方法
@staticmethod
def Breath():
print ‘breathing...‘
#装饰器
@property
def Sing(self):
print self.name+‘ singing...‘
#只读私有字段
@property
def ShowGender(self):
return self.__gender
#可改私有字段
@ShowGender.setter
def ShowGender(self,value):
self.__gender=value
#man继承Person类,私有方法__gender无法继承
class man(Person):
#重写__init__方法
def __init__(self):
self.name=‘cxiong‘
self.__gender=‘male‘
#新增work方法
def work(self):
print ‘work hard...‘
m1=man()
m1.work()
m1.Sing
m1.Breath()
7、新式类与经典类的区别
新式类:继承object
经典类:不继承object
使用新式类的原因:经典类存在多继承bug,深度优先,而非广度优先,请参考以下内容
https://docs.python.org/release/2.2.3/whatsnew/sect-rellinks.html
[root@localhost ~]# cat index1.py
#!/usr/bin/python27
#coding:utf-8
class Person(object):
#静态字段
nation=‘中国‘
#构造函数
def __init__(self,name,age,gender):
#动态字段
self.name=name
self.age=age
#私有字段
self.__gender=gender
print ‘person init...‘
#析构函数
def __del__(self):
print ‘解释器要销毁person了...‘
#动态方法
def Eat(self):
print self.name+‘ eating...‘
#
def __call__(self):
print ‘call‘
#静态方法
@staticmethod
def Breath():
print ‘breathing...‘
#装饰器
@property
def Sing(self):
print self.name+‘ singing...‘
#只读私有字段
@property
def ShowGender(self):
return self.__gender
#可改私有字段
@ShowGender.setter
def ShowGender(self,value):
self.__gender=value
#man继承Person类
class man(Person):
#重写__init__方法
def __init__(self):
self.name=‘cxiong‘
self.__gender=‘male‘
print ‘man init...‘
#调用父类的init方法1
Person.__init__(self,‘cxiong‘,29,‘male‘)
#调用父类的init方法2
super(man,self).__init__(‘cxiong‘,29,‘male‘)
#新增work方法
def work(self):
print ‘work hard...‘
m1=man()
m1.work()
m1.Sing
m1.Breath()
[root@localhost ~]# python index1.py
man init...
person init...
person init...
work hard...
cxiong singing...
breathing...
解释器要销毁person了...
[root@localhost ~]#
8、多继承
python特有的特性
[root@localhost ~]# python multiple.py
this is D
save method from --A--
[root@localhost ~]# cat multiple.py
#!/usr/bin/python27
#coding:utf-8
class A:
def __init__(self):
print ‘this is A‘
def save(self):
print ‘save method from --A--‘
class B(A):
def __init__(self):
print ‘this is B‘
class C(A):
def __init__(self):
print ‘this is C‘
def save(self):
print ‘save method from --C--‘
class D(B,C):
def __init__(self):
print ‘this is D‘
c=D()
c.save()
[root@localhost ~]# vim multiple.py
[root@localhost ~]# python multiple.py
this is D
save method from --C--
[root@localhost ~]# cat multiple.py
#!/usr/bin/python27
#coding:utf-8
class A(object):
def __init__(self):
print ‘this is A‘
def save(self):
print ‘save method from --A--‘
class B(A):
def __init__(self):
print ‘this is B‘
class C(A):
def __init__(self):
print ‘this is C‘
def save(self):
print ‘save method from --C--‘
class D(B,C):
def __init__(self):
print ‘this is D‘
c=D()
c.save()
[root@localhost ~]#
9、接口
规范:抽象类+抽象方法=接口
from abc import ABCMeta, abstractmethod
class Bar:
__metaclass__ = ABCMeta
@abstractmethod
def Fun(self):pass
class Foo(Bar):
def __init__(self):
print ‘__init__‘
Foo()
python-装饰器,类与对象,私有字段,析构,__call__,继承,多继承,接口