python面向对象--类的特殊成员&&如何调用其他类的成员


1     主动调用其他类的成员

若子类和父类有相同的方法,那么该如何都调用呢?

1.1   方式一  :类.方法(self)

 1 class Base(object):
 2 
 3     def f1(self):
 4         print('5个功能')
 5 
 6 class Foo(object):
 7 
 8     def f1(self):
 9         print('3个功能')
10         Base.f1(self)
11 
12 obj = Foo()
13 obj.f1()
View Code

注意:类.方法(self)----需要自己写参数

 

1.2   方式二:按照类的继承顺序,找下一个

 1 class Foo(object):
 2 
 3     def f1(self):
 4 
 5         super().f1()
 6 
 7         print('3个功能')
 8 
 9 
10 class Bar(object):
11 
12     def f1(self):
13 
14         print('6个功能')
15 
16  
17 
18 class Info(Foo,Bar):
19 
20     pass
21 
22  
23 
24 obj = Info()
25 
26 obj.f1()
View Code

注意:按照类的继承顺序,找下一个。而不是按照继承关系找

super().f1()和self.f1()方式不同,self每次得回Info去寻找,而super直接按照Info--->Foo--->Bar顺序执行

 

2     类的特殊成员

这些特殊成员并没有特殊的功能,仅仅是某些外部的操作触发了特定的特殊成员

2.1   __init__()  &&  __new__()    构造方法

类名() -------》自动执行__new__/ __init__

 

 1 class Foo:
 2 
 3     def __init__(self,a1,a2):       #初始化方法
 4         #为空对象进行数据初始化
 5         self.a1 = a1
 6         self.a2 = a2
 7         print(2)
 8 
 9     def __new__(cls, *args, **kwargs):   #构造方法
10         #创建一个空对象
11         print(1,args,kwargs)
12         return object.__new__(cls)      #python内部创建一个当前类的对象(初创时内部是空的)
13 
14 obj = Foo(3,4)
15 #1 (3, 4) {}
16 #2
View Code

注意:1:__new__()必须要有返回值,返回一个对象,因此传给__init__的self就是一个对象

2:先执行__new__()后执行__init__()

3:所有的对象的创建都是由object创建的

      4:注意:并不是__new__()就是构造函数的意思,是因为obj = Foo(3,4)触发了__new__()的自动执行

 

2.2   __call__()

对象() -------》自动执行__call__

 1 class Foo:
 2     def __init__(self,a1,a2):       #初始化方法
 3         #为空对象进行数据初始化
 4         self.a1 = a1
 5         self.a2 = a2
 6         print(2)
 7 
 8     def __call__(self, *args, **kwargs):
 9         print(111,args,kwargs)
10 
11 obj = Foo(3,4)
12 ret = obj()
View Code

 

2.3   __getitem__()

对象['xx'] ------》 自动执行 __getitem__

 

 1 class Foo:
 2 
 3     def __init__(self,a1,a2):       #初始化方法
 4         #为空对象进行数据初始化
 5         self.a1 = a1
 6         self.a2 = a2
 7         print(2)
 8 
 9     def __getitem__(self, item):
10         print(item)
11 
12 obj = Foo(3,4)
13 ret = obj[5]
View Code 

 

2.4   __setitem__()   

对象['xx'] = xxx     自动执行 __setitem__

 1 class Foo:
 2 
 3     def __init__(self,a1,a2):       #初始化方法
 4         #为空对象进行数据初始化
 5         self.a1 = a1
 6         self.a2 = a2
 7         print(2)
 8     def __setitem__(self, key, value):
 9         print(key,value)
10         return 1
11 
12 obj = Foo(1,2)
13 obj['k1'] = 123
14 
15 # k1 123
View Code

注意:没有返回值

 

2.5   __delitem__() 

del 对象[xx]     自动执行 __delitem__

 1 class Foo:
 2     def __init__(self,a1,a2):       #初始化方法
 3         #为空对象进行数据初始化
 4         self.a1 = a1
 5         self.a2 = a2
 6         print(2)
 7 
 8     def __delitem__(self, key):
 9         print(key)
10         return 1
11 
12 obj = Foo(1,2)
13 del obj['uuu']
View Code

注意:无返回值

 

2.6   __add__() 

对象+对象     自动执行 __add__

 1 class Foo(object):
 2 
 3     def __init__(self, a1, a2):
 4         self.a1 = a1
 5         self.a2 = a2
 6 
 7     def __add__(self, other):
 8         return self.a1 + other.a2
 9 
10 obj1 = Foo(1,2)
11 obj2 = Foo(3,4)
12 ret = obj1 + obj2
13 print(ret)
View Code

注意:obj1,obj2  与  self, other两两对应

 

2.7   __enter__() && __exit__()

with 对象        自动执行 __enter__ / __exit__

 

 1 class Foo(object):
 2     def __init__(self, a1, a2):
 3         self.a1 = a1
 4         self.a2 = a2
 5 
 6     def __enter__(self):
 7         print('1111')
 8         return 999
 9 
10     def __exit__(self, exc_type, exc_val, exc_tb):
11         print('22222')
12 
13 obj = Foo(1,2)
14 with obj as f:
15     print(f)
16     print('内部代码')
17 
18  
19 
20 答案:
21 
22 '''
23 1111
24 999
25 内部代码
26 22222
27 '''
View Code

 

2.8   __str__()

 作用:

把打印的对象变成字符串

 

 用法举例:

 1 class Foo(object):
 2     def __init__(self):
 3         pass
 4     def func(self):
 5         pass
 6     def __str__(self):
 7         return 'F1'
 8 
 9 obj = Foo()
10 print(obj)
View Code

 

2.9   __iter__()

 作用:

如果想要把不可迭代对象 -> 可迭代对象

1. 在类中定义__iter__方法

2. iter内部返回一个迭代器(生成器也是一种特殊迭代器)

 

 用法举例:

 1 class Foo(object):
 2     def __init__(self,name,age):
 3         self.name = name
 4         self.age = age
 5 
 6     def func(self):
 7         pass
 8     def __iter__(self):
 9         # return  iter([1,2])     #用法一
10         yield 1                   #用法二
11         yield 2
12 
13 obj1 = Foo('刘we',99)
14 for item in obj1:
15     print(item)
16 
17 注意:iter()相当于for  xx in ,这个可以实验一下
View Code

return  iter()------->就是把可迭代对象转化成迭代器返回

 

 

2.10   __dcit__

作用:

把对象封装的所有值以字典拿到

 用法举例:

 1 class Foo(object):
 2     def __init__(self,name,age):
 3         self.name = name
 4         self.age = age
 5         self.s = None
 6 
 7     def func(self):
 8         pass
 9 
10 obj1 = Foo('liu',99)
11 obj2 = Foo('op',89)
12 
13 print(obj1.__dict__)
14 print(obj2.__dict__)
15 # {'name': 'liu', 'age': 99, 's': None}
16 # {'name': 'op', 'age': 89, 's': None}
View Code

 

2.11  __doc__

作用:获取注释信息

 用法举例:

 1 class Foo:
 2     '''
 3     asfffgfg
 4     '''
 5     def __init__(self,name):
 6         self.name = name
 7     def func(self):
 8         pass
 9 
10 obj  = Foo(1)
11 print(obj.__doc__)      #asfffgfg
View Code

2.12 __getattr__()

作用:对象.x

1 class Local(object):
2     def __getattr__(self, item):
3         return 333
4 
5 obj = Local()
6 print(obj.x)    #333
View Code

 2.13 __setattr__()

作用:对象.x = 值

 

 1 class Foo():
 2     def __init__(self):
 3         self.name = 'alex'  #默认自己类中执行__setattr__
 4 
 5     # def __setattr__(self, key, value):
 6     #     pass
 7     #     object.__setattr__(self, key, value)
 8 
 9 
10 obj = Foo()
11 print(obj.name)
12 #对象.x默认先执行自己类里面的__setattr__方法,自己类里面没有,则去父类里面找。
13 # 对象在实例化过程中默认先执行__new__方法,然后在执行__init__方法
View Code

 

 2.14 __eq__

 

 2.15 __hash__

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM