本节内容:
面向对象高级语法部分经典类vs新式类 静态方法、类方法、属性方法类的特殊方法反射Socket开发基础作业:开发一个支持多用户在线的FTP程序面向对象高级语法部分经典类vs新式类
把下面代码用python2 和python3都执行一下
class A:def init(self):
self.n = 'A'
class B(A):
def init(self):# self.n = 'B'pass
class C(A):
def init(self):
self.n = 'C'
class D(B,C):
def init(self):# self.n = 'D'pass
obj = D()
print(obj.n)
classical vs new style:
经典类:深度优先新式类:广度优先super()用法抽象接口class Alert(object):'''报警基类'''
__Metaclass__ = abc.ABCMeta
@abc.abstractmethoddef send(self): '''报警消息发送接口''' pass
class MailAlert(Alert):
pass
m = MailAlert()
m.send()
上面的代码仅在py2里有效,python3里怎么实现呢?
静态方法
通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类变量,但静态方法是不可以访问实例变量或类变量的,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什么关系了,它与类唯一的关联就是需要通过类名来调用这个方法
def <a href="https://www.jb51.cc/tag/init/" target="_blank" >__init__</a>(self,<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>): self.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> = <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>@stati<a href="https://www.jb51.cc/tag/cme/" target="_blank" >cme</a>thod #把eat<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" >方法</a>变为静态<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" >方法</a>def eat(self): print("<a href="https://www.jb51.cc/tag/s/" target="_blank" >%s</a> is eating" % self.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>)
d = Dog("ChenRonghua")
d.eat()
上面的调用会出以下错误,说是eat需要一个self参数,但调用时却没有传递,没错,当eat变成静态方法后,再通过实例调用时就不会自动把实例本身当作一个参数传给self了。
d.eat()TypeError: eat() missing 1 required positional argument: 'self'想让上面的代码可以正常工作有两种办法
1. 调用时主动传递实例本身给eat方法,即d.eat(d)
2. 在eat方法中去掉self参数,但这也意味着,在eat中不能通过self.调用实例中的其它变量了
self.name = ( d = Dog( d.eat()类方法类方法通过@classmethod装饰器实现,类方法和普通方法的区别是, 类方法只能访问类变量,不能访问实例变量
@classmethoddef eat(self): print("<a href="https://www.jb51.cc/tag/s/" target="_blank" >%s</a> is eating" % self.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>)
d = Dog("ChenRonghua")
d.eat()
执行报错如下,说Dog没有name属性,因为name是个实例变量,类方法是不能访问实例变量的
d.eat() file "/Users/jIEli/PycharmProjects/python基础/自动化day7面向对象高级/类方法.py",line 11,in eat print("%s is eating" % self.name)AttributeError: type object 'Dog' has no attribute 'name'此时可以定义一个类变量,也叫name,看下执行效果
@classmethoddef eat(self): print("<a href="https://www.jb51.cc/tag/s/" target="_blank" >%s</a> is eating" % self.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>)
d = Dog("ChenRonghua")
d.eat()
我是类变量 is eating
属性方法属性方法的作用就是通过@property把一个方法变成一个静态属性
def <a href="https://www.jb51.cc/tag/init/" target="_blank" >__init__</a>(self,<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>): self.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> = <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>@propertydef eat(self): print(" <a href="https://www.jb51.cc/tag/s/" target="_blank" >%s</a> is eating" <a href="https://www.jb51.cc/tag/s/" target="_blank" >%s</a>elf.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>)
d = Dog("ChenRonghua")
d.eat()
d.eat()TypeError: 'nonetype' object is not callable
正常调用如下
输出ChenRonghua is eating
好吧,把一个方法变成静态属性有什么卵用呢?既然想要静态变量,那直接定义成一个静态变量不就得了么?well,以后你会需到很多场景是不能简单通过 定义 静态属性来实现的, 比如 ,你想知道一个航班当前的状态,是到达了、延迟了、取消了、还是已经飞走了, 想知道这种状态你必须经历以下几步:
1. 连接航空公司API查询
2. 对查询结果进行解析
3. 返回结果给你的用户
因此这个status属性的值是一系列动作后才得到的结果,所以你每次调用时,其实它都要经过一系列的动作才返回你结果,但这些动作过程不需要用户关心, 用户只需要调用这个属性就可以,明白 了么?
=</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">def</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> checking_status(self): </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">checking f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght <a href="https://www.jb51.cc/tag/s/" target="_blank" >%s</a> status </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span> %<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> self.f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght_<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span> 1<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">@property</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">def</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght_status(self): status </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> self.checking_status() </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> status ==<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> 0 : </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght got canceled...</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">e<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>f</span> status == 1<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> : </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght is arrived...</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">e<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>f</span> status == 2<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght has departured already...</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">cannot confirm the f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght status...,please check later</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)
f
= Flight(<span >"<span >CA980<span >"<span >)f.flight_status
cool,那现在我只能查询航班状态, 既然这个flight_status已经是个属性了, 那我能否给它赋值呢?试试吧
输出, 说不能更改这个属性,我擦。。。。,怎么办怎么办。。。
f.flight_status = 2AttributeError: can't set attribute当然可以改, 不过需要通过@proerty.setter装饰器再装饰一下,此时 你需要写一个新方法, 对这个flight_status进行更改。
=</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">def</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> checking_status(self): </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">checking f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght <a href="https://www.jb51.cc/tag/s/" target="_blank" >%s</a> status </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span> %<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> self.f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght_<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span> 1<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">@property</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">def</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght_status(self): status </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> self.checking_status() </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> status ==<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> 0 : </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght got canceled...</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">e<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>f</span> status == 1<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> : </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght is arrived...</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">e<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>f</span> status == 2<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght has departured already...</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">cannot confirm the f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght status...,please check later</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)@f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght_status.setter </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><a href="https://www.jb51.cc/tag/xiugai/" target="_blank" >修改</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">def</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght_status(self,status): status_dic </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> { 0 : </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">canceled</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,</span>1 :<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">arrived</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,</span>2 : <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">departured</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">3[31;1mHas changed the f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght status to 3[0m</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,status_dic.get(status) )@f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght_status.deleter </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><a href="https://www.jb51.cc/tag/shanchu/" target="_blank" >删除</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">def</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> f<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ght_status(self): </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">status got removed...</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)
f
= Flight(<span >"<span >CA980<span >"<span >)f.flight_status
f.flight_status = 2 <span >#<span >触发@flight_status.setter
<span >del f.flight_status <span >#<span >触发@flight_status.deleter
注意以上代码里还写了一个@flight_status.deleter,是允许可以将这个属性删除
类的特殊成员方法1. __doc__ 表示类的描述信息def func(self): pass
print Foo.doc
输出:类的描述信息2. __module__ 和 __class____module__ 表示当前 *** 作的对象在那个模块
__class__ 表示当前 *** 作的对象的类是什么
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">def</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;"><a href="https://www.jb51.cc/tag/init/" target="_blank" >__init__</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">(self): self.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">'</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">wupeiqi</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">'</span></pre>
lib.aa obj =<span > C()<span >print obj.<span >module__ <span >#<span > 输出 lib.aa,即:输出模块
<span >print obj.<span >class__ <span >#<span > 输出 lib.aa.C,即:输出类3. __init__ 构造方法,通过类创建对象时,自动触发执行。4.__del__
析构方法,当对象在内存中被释放时,自动触发执行。
注:此方法一般无须定义,因为Python是一门高级语言,程序员在使用时无需关心内存的分配和释放,因为此工作都是交给Python解释器来执行,所以,析构函数的调用是由解释器在进行垃圾回收时自动触发执行的
5. __call__ 对象后面加括号,触发执行。
注:构造方法的执行是由创建对象触发的,即:对象 = 类名() ;而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()()
def <a href="https://www.jb51.cc/tag/init/" target="_blank" >__init__</a>(self): passdef __call__(self,*args,**<a href="https://www.jb51.cc/tag/kwargs/" target="_blank" >kwargs</a>): print '__call__'
obj = Foo() # 执行 init
obj() # 执行 call
country = 'China'def <a href="https://www.jb51.cc/tag/init/" target="_blank" >__init__</a>(self,<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>,count): self.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> = <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> self.count = countdef func(self,**<a href="https://www.jb51.cc/tag/kwargs/" target="_blank" >kwargs</a>): print 'func'
获取类的成员,即:静态字段、方法、print Province.dict
输出:{'country': 'China','module': 'main__','func': <function func at 0x10be30f50>,'init': <function init at 0x10be30ed8>,'doc__': None}obj1 = Province('HeBei',10000)
print obj1.dict
obj2 = Province('HeNan',3888)
print obj2.dict
def __str__(self): return 'alex <a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>'
obj = Foo()
print obj
用于索引 *** 作,如字典。以上分别表示获取、设置、删除数据
def __getitem__(self,key): print('__getitem__',key)def __setitem__(self,key,value): print('__setitem__',value)def __de<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>tem__(self,key): print('__de<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>tem__',key)
obj = Foo()
result = obj['k1'] # 自动触发执行 getitem
obj['k2'] = 'alex' # 自动触发执行 setitem
del obj['k1']
def <a href="https://www.jb51.cc/tag/init/" target="_blank" >__init__</a>(self,<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>): self.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> = <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>
f = Foo("alex")
上述代码中,obj 是通过 Foo 类实例化的对象,其实,不仅 obj 是一个对象,Foo类本身也是一个对象,因为在Python中一切事物都是对象。
如果按照一切事物都是对象的理论:obj对象是通过执行Foo类的构造方法创建,那么Foo类对象应该也是通过执行某个类的 构造方法 创建。
表示,obj 对象由Foo类创建print type(Foo) # 输出:所以,f对象是Foo类的一个实例,Foo类对象是 type 类的一个实例,即:Foo类对象 是通过type类的构造方法创建。
那么,创建类就可以有两种方式:
a). 普通方式
def func(self): print 'hello alex'
b). 特殊方式
Foo = type('Foo',(object,),{'func': func})type第一个参数:类名type第二个参数:当前类的基类type第三个参数:类的成员(%<span >def <span >init<span >(self,age):self.name =<span > name
self.age =<span > age
Foo = type(<span >'<span >Foo
f
= Foo(<span >"<span >jack<span >",22<span >)f.func()
那么问题来了,类默认是由 type 类实例化产生,type类中如何实现的创建类?类又是如何创建对象?
答:类中有一个属性 __Metaclass__,其用来表示该类由 谁 来实例化创建,所以,我们可以为 __Metaclass__ 设置一个type类的派生类,从而查看 类 创建的过程。
<div onclick="cnblogs_code_show('c883737b-8a27-46f7-9a50-7a720dc65a9f')">
<img ID="code_img_closedc883737b-8a27-46f7-9a50-7a720dc65a9f" src="https://www.jb51.cc/res/2019/02-10/23/1c53668bcee393edac0d7b3b3daff1ae.gif" alt=""><img ID="code_img_openedc883737b-8a27-46f7-9a50-7a720dc65a9f" onclick="cnblogs_code_hIDe('c883737b-8a27-46f7-9a50-7a720dc65a9f',event)" src="https://www.jb51.cc/res/2019/02-10/23/405b18b4b6584ae338e0f6ecaf736533.gif" alt=""><div ID="cnblogs_code_open_c883737b-8a27-46f7-9a50-7a720dc65a9f" >
(self,** (,** (self,** (,** obj = self. (,obj,** self.(obj,** (cls,** (,** type.(cls,** ( Foo(object,Metaclass= self.name = ( (cls,** (,cls,** object. f = Foo( ( (,f.name)
类的生成 调用 顺序依次是 __new__ --> __init__ --> __call__
getattr(object,default=None): value
Get a <a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>d attribute from an object; getattr(x,'y') is equivalent to x.y.When a defa<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>t argument is given,it is returned when the attribute doesn'texist; without it,an exception is raised in that case.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"""</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pass</span></pre>
判断object中有没有一个name字符串对应的方法或属性 setattr(x,y,v): setattr(x,'y',v) is equivalent to ``x.y = v''</span></pre>
delattr(x,y): delattr(x,'y') is equivalent to ``del x.y''</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"""</span></pre>
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">def</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;"><a href="https://www.jb51.cc/tag/init/" target="_blank" >__init__</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">(self): self.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">'</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">wupeiqi</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">'</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">def</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> func(self): </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">'</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">func</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">'</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">
obj
=<span > Foo()<span >#<span > #### 检查是否含有成员 ####hasattr(obj,<span >'<span >name<span >'<span >)
hasattr(obj,<span >'<span >func<span >'<span >)<span >#<span > #### 获取成员 ####
getattr(obj,<span >'<span >name<span >'<span >)
getattr(obj,<span >'<span >func<span >'<span >)<span >#<span > #### 设置成员 ####
setattr(obj,<span >'<span >age<span >',18<span >)
setattr(obj,<span >'<span >show<span >',<span >lambda num: num + 1<span >)<span >#<span > #### 删除成员 ####
delattr(obj,<span >'<span >name<span >'<span >)
delattr(obj,<span >'<span >func<span >')
<div >
<pre >import importlib
__import__('import_lib.Metaclass') #这是解释器自己内部用的
importlib.import_module('import_lib.Metaclass') #与上面这句效果一样,官方建议用这个异常处理
参考 http://www.cnblogs.com/wupeiqi/articles/5017742.HTML
Socket 编程总结
以上是内存溢出为你收集整理的Python之路,Day7 - 面向对象编程进阶全部内容,希望文章能够帮你解决Python之路,Day7 - 面向对象编程进阶所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)