Python面向对象(特殊成员)

day25

    __init__     类()自动执行
    __del__
    __call__     对象()  类()() 自动执行
    __int__      int(对象)  
    __str__      str()

特殊成员

 

 1 class Foo:
 2     def __init__(self):
 3         print('init')
 4 
 5     def __call__(self, *argc, **kwarge):
 6         print('call')
 7 
 8 obj = Foo()#直接执行__init__
 9 
10 obj()#执行__call__中内容
11 
12 Foo()()#同上

 

对象加括号,obj()执行__call__()。

执行结果:

init
call
init
call

Process finished with exit code 0

 

 

__str__,__int__

 

 1 class Foo:
 2     def __init__(self):
 3         pass
 4 
 5     def __int__(self):
 6         return 111
 7 
 8     def __str__(self):
 9         return "nizhipeng"
10 obj = Foo()
11 
12 print(obj, type(obj))
13 
14 r = int(obj)#自动执行__int__方法,并将返回值给int对象
15 print(r)
16 
17 print(str(obj))

 

类型转换的时候,自动执行__int__,和__str__方法。

执行结果:

nizhipeng <class '__main__.Foo'>
111
nizhipeng

Process finished with exit code 0

 

 

自动转换

 

1 class Foo:
2     def __init__(self, n, a):
3         self.name = n
4         self.age = a
5     def __str__(self):
6         return '%s-%s' %(self.name, self.age)
7 
8 obj = Foo('alex', 18)
9 print(obj) #obj自动转成了str类型

 

第9行自动执行了str(obj)。

执行结果:

alex-18

Process finished with exit code 0

 

__add__

 1 class Foo:
 2 
 3     def __init__(self, name, age):
 4         self.name = name
 5         self.age = age
 6 
 7     def __add__(self, other):
 8         #self为obj1包含信息(alex,18)
 9         #other为obj2包含信息(eiro,66)
10 
11         '''return self.age + other.age'''
12         return Foo(obj1.name, other.age)#对象1的名字,对象2的年龄
13 
14     def __del__(self):
15         print("析构方法")#对象销毁时执行
16 
17 
18 obj1 = Foo('alex', 19)
19 obj2 = Foo('eiro', 66)
20 
21 r = obj1 + obj2#两个对象相加时,自动执行第一个对象的__add__方法,并且将第二个对象当作参数
22 
23 print(r.age, type(r))
两个对象相加时,自动执行第一个对象的__add__方法,并且将第二个对象当作参数
执行结果:
66 <class '__main__.Foo'>
析构方法
析构方法
析构方法

Process finished with exit code 0

 

__dict__
 1 class Foo:
 2 
 3     '''
 4     注释也是类成员
 5     '''
 6     def __init__(self, name, age):
 7         self.name = name
 8         self.age = age
 9         self.n = 123
10 
11 obj = Foo('alex', 18)
12 
13 d = obj.__dict__#通过字典形式显示对象成员
14 print(d)
15 
16 ret = Foo.__dict__#通过字典形式显示类成员
17 print(ret)

__dict__()可以通过字典显示类成员,和对象成员。注释也是类成员的一部分。

执行结果:

{'name': 'alex', 'age': 18, 'n': 123}
{'__init__': <function Foo.__init__ at 0x7fb7f3155620>, '__doc__': '\n    注释也是类成员\n    ', '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__module__': '__main__'}
44

Process finished with exit code 0

 

__getitem__,__setitem__,__delitem__

 1 class Foo:
 2     def __init__(self, name, age):
 3         self.name = name
 4         self.age = age
 5 
 6     def __getitem__(self, item):
 7         return item+10
 8 
 9     def __setitem__(self, key, value):
10         print(key, value)
11 
12     def __delitem__(self, key):
13         print(key)
14 li = Foo('alex', 18)
15 #以索引方式访问对象,执行__getitem__方法
16 
17 r = li[8]#自动执行li对象类中的__getitem__方法, 8作为参数给item
18 print(r)#有获取数据属于需要返回值
19 
20 li[100] = 'aasd'#执行__setitem__(), key为100,value为aasd
21 
22 del li[999]#执行__delitem__()

以索引方式访问需要以上几种特殊方法。

__setitem__,__delitem__并不获取数据,不需要return。

执行结果:

18
100 aasd
999

Process finished with exit code 0

 

__iter__

 1 class Foo:
 2     def __init__(self, name, age):
 3         self.name = name
 4         self.age = age
 5 
 6     def __iter__(self):
 7         return iter([11,22,33])#生成迭代器对象
 8 #如果类中有__iter__方法,对象为可迭代对象
 9 #对象.__iter__()的返回值:迭代器
10 li = Foo('alex', 18)
11 
12 
13 for i in li:
14     print(i)
如果类中有__iter__方法,对象为可迭代对象,iter()生成迭代对象。

执行结果:

11
22
33

Process finished with exit code 0

 

__new__ 和 __metaclass__

Foo类其实为type类的对象。

所有类默认继承object类。

 

http://www.cnblogs.com/wupeiqi/p/4766801.html

其中第11节

 

 1 class MyType(type):
 2     #self 为 Foo
 3     def __init__(self, *args, **kwargs):
 4         print('123')
 5         pass
 6 
 7     def __call__(cls):
 8         print('456')
 9 
10 #Foo为MyType对象
11 class Foo(object,metaclass=MyType):#任何类都继承object类,并使Foo类为type类子类MyType的对象
12     def __init__(self):
13         print('567')
14 
15     def func(self):
16         print('hello')
17 
18 #使Foo类为type类子类MyType的对象,创建Foo对象时会执行类中的__init__()方法
19 #输出  123
20 
21 #有__call__方法时输出456,对象加(),执行__call__方法,Foo是MyType的一个对象。
22 #没有时输出567,Foo类创建obj对象,执行__init__方法。
23 obj = Foo()
任何类都继承object类,并使Foo类为type类子类MyType的对象
执行结果:
123
456

Process finished with exit code 0

 

转载于:https://www.cnblogs.com/112358nizhipeng/p/9835980.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值