特殊成员,主动调用其他类的成员

一.组合的补充

1 class Foo:
2     def __init__(self):
3         pass
4 user_info={
5     Foo:1,
6     Foo():2
7 }
8 print(user_info)  #{<class '__main__.Foo'>: 1, <__main__.Foo object at 0x0000012DDE6D8BE0>: 2}
1.类和对象是否能做字典的key
 1     def __init__(self,age):
 2         self.age=age
 3     def display(self):
 4         print(self.age)
 5 data_list=[Foo(8),Foo(9)]
 6 for item in data_list:
 7     print(item.age,item.display())
 8 # 8
 9 # 8 None
10 # 9
11 # 9 None
2.对象中到底有什么
 1 class StarkConfig():
 2 
 3     def __init__(self,num):
 4         self.num=num
 5 
 6     def changelist(self,request):
 7         print(self.num,request)
 8 
 9 class RoleConfig(StarkConfig):
10 
11     def changelist(self,request):
12         print(666)
13 
14 config_obj_list=[StarkConfig(1),StarkConfig(2),RoleConfig(3)]
15 
16 for item in config_obj_list:
17     print(item.num)
18 # 1,2,3
3.练习题
 1 class StarkConfig():
 2 
 3     def __init__(self,num):
 4         self.num=num
 5 
 6     def changelist(self,request):
 7         print(self.num,request)
 8 
 9 class RoleConfig(StarkConfig):
10 
11     def changelist(self,request):
12         print(666)
13 
14 config_obj_list=[StarkConfig(1),StarkConfig(2),RoleConfig(3)]
15 
16 for item in config_obj_list:
17     item.changelist(168)
18 # 1 168
19 # 2 168
20 # 666
4.练习题
 1 class StarkConfig():
 2 
 3     def __init__(self,num):
 4         self.num=num
 5 
 6     def changelist(self,request):
 7         print(self.num,request)
 8 
 9 class RoleConfig(StarkConfig):
10 
11     def changelist(self,request):
12         print(666,self.num)
13 
14 config_obj_list=[StarkConfig(1),StarkConfig(2),RoleConfig(3)]
15 
16 for item in config_obj_list:
17     item.changelist(168)
18 # 1 168
19 # 2 168
20 # 666 3
5.练习题
 1 class StarkConfig():
 2 
 3     def __init__(self,num):
 4         self.num=num
 5 
 6     def changelist(self,request):
 7         print(self.num,request)
 8 
 9     def run(self):
10         self.changelist(999)
11 
12 class RoleConfig(StarkConfig):
13 
14     def changelist(self,request):
15         print(666,self.num)
16 
17 config_obj_list=[StarkConfig(1),StarkConfig(2),RoleConfig(3)]
18 
19 config_obj_list[1].run() #2 999
20 config_obj_list[2].run()  #666 3
6.练习题
 1 class StarkConfig():
 2 
 3     def __init__(self,num):
 4         self.num=num
 5 
 6     def changelist(self,request):
 7         print(self.num,request)
 8 
 9     def run(self):
10         self.changelist(999)
11 
12 class RoleConfig(StarkConfig):
13 
14     def changelist(self,request):
15         print(666,self.num)
16 
17 class AdminSite:
18     def __init__(self):
19         self._registry={}
20 
21     def register(self,k,v):
22         self._registry[k]=v
23 
24 site=AdminSite()
25 print(len(site._registry))  #0
26 site.register('range',666)
27 site.register('shili',438)
28 print(len(site._registry))  #2
29 
30 site.register('lyd',StarkConfig(19))
31 site.register('yjl',StarkConfig(20))
32 site.register('fgz',RoleConfig(33))
33 
34 print(len(site._registry)) # 5
7.练习题
 1 class StarkConfig():
 2 
 3     def __init__(self,num):
 4         self.num=num
 5 
 6     def changelist(self,request):
 7         print(self.num,request)
 8 
 9     def run(self):
10         self.changelist(999)
11 
12 class RoleConfig(StarkConfig):
13 
14     def changelist(self,request):
15         print(666,self.num)
16 
17 class AdminSite:
18     def __init__(self):
19         self._registry={}
20 
21     def register(self,k,v):
22         self._registry[k]=v
23 
24 site=AdminSite()
25 
26 site.register('lyd',StarkConfig(19))
27 site.register('yjl',StarkConfig(20))
28 site.register('fgz',RoleConfig(33))
29 
30 print(len(site._registry)) # 3
31 
32 for k,row in site._registry.items():
33     row.changelist(5)
34 
35 # 19 5
36 # 20 5
37 # 666 33
8.练习题
 1 class StarkConfig():
 2 
 3     def __init__(self,num):
 4         self.num=num
 5 
 6     def changelist(self,request):
 7         print(self.num,request)
 8 
 9     def run(self):
10         self.changelist(999)
11 
12 class RoleConfig(StarkConfig):
13 
14     def changelist(self,request):
15         print(666,self.num)
16 
17 class AdminSite:
18     def __init__(self):
19         self._registry={}
20 
21     def register(self,k,v):
22         self._registry[k]=v
23 
24 site=AdminSite()
25 
26 site.register('lyd',StarkConfig(19))
27 site.register('yjl',StarkConfig(20))
28 site.register('fgz',RoleConfig(33))
29 
30 print(len(site._registry)) # 3
31 
32 for k,row in site._registry.items():
33     row.run()
34 
35 # 19 999
36 # 20 999
37 # 666 33
9.练习题
 1 class UserInfo:
 2     pass
 3 class Department:
 4     pass
 5 
 6 class StarkConfig():
 7 
 8     def __init__(self,num):
 9         self.num=num
10 
11     def changelist(self,request):
12         print(self.num,request)
13 
14     def run(self):
15         self.changelist(999)
16 
17 class RoleConfig(StarkConfig):
18 
19     def changelist(self,request):
20         print(666,self.num)
21 
22 class AdminSite:
23     def __init__(self):
24         self._registry={}
25 
26     def register(self,k,v):
27         self._registry[k]=v(k)
28 
29 site=AdminSite()
30 
31 site.register(UserInfo,StarkConfig)
32 site.register(Department,StarkConfig)
33 
34 print(len(site._registry)) # 3
35 
36 for k,row in site._registry.items():
37     row.run()
38 
39 # <class '__main__.UserInfo'> 999
40 # <class '__main__.Department'> 999
10.练习题

二.主动调用其他类的成员

 1 class Base:
 2     def func(self):
 3         print(666)
 4 
 5 方案一:
 6 obj=Base()
 7 obj.func()
 8 
 9 方案二:
10 obj=Base()
11 Base.func(obj)
 1 class Base:
 2     def f1(self):
 3         print('五个功能')
 4 
 5 class Foo(Base):
 6     def f1(self):
 7         print('三个功能')
 8         Base.f1(self)  #类名.函数名(self/对象名)
 9 
10 obj=Foo()
11 obj.f1()
方法一:
 1 class Base:
 2     def f1(self):
 3         print('五个功能')
 4 
 5 class Foo(Base):
 6     def f1(self):
 7         ret= super().f1()  #按照类的继承顺序,找下一个
 8         print(ret)
 9         print('三个功能')
10 
11 obj=Foo()
12 obj.f1()
方法二:

三.特殊成员

 1 class Foo:
 2     def __init__(self,a1,a2):
 3         self.a1=a1
 4         self.a2=a2
 5         print(666)
 6     def __call__(self,*args,**kwargs):
 7         print(111,args,kwargs)
 8         return 123
 9     def __getitem__(self, item):
10         print(item)
11         return 8
12     def __setitem__(self, key, value):
13         print(key, value)
14         return 123
15     def __delitem__(self, key):
16         print(key)
17     def __add__(self,other):
18         return self.a1+other.a2
19 
20     def __enter__(self):
21         print('先执行')
22         return 999  #可以有返回值
23     def __exit__(self, exc_type, exc_val, exc_tb):
24         print('后执行')
25     def __new__(cls, *args, **kwargs):
26         pass
1 obj=Foo(1,2)  #666
1.类名() 自动执行__init__
1 print(obj(1,2,3,a=1))   ## 111 (1, 2, 3) {'a': 1}   123
2.对象() 自动执行__call__
1 ret=obj['shy']   #shy
2 print(ret)   #8
3.对象[] 自动执行__getitem__
1 ret=obj[11]=11  #11 11
2 print(ret)   #返回值与key和value没有关系
4.对象['xx']=xx 自动执行__setitem__
1 del obj['uuu']   #uuu   没有返回值
5.del 对象[xx] 自动执行__delitem__
1 obj1=Foo(1,2)
2 obj2=Foo(88,99)
3 ret=obj1+obj2
4 print(ret)  #100
6.对象+对象 自动执行 __add__ 对象之间的相加(加减乘除绝对值等等全部都有)
1 obj=Foo(1,2)
2 with obj as f:
3     print(f)
4     print('中间代码')
7.with 对象: 自动执行 __enter__,__exit__
 1 class Foo(object):
 2     def __init__(self,a1,a2):
 3         '''
 4         为对象进行数据的初始化
 5         :param a1:
 6         :param a2:
 7         '''
 8         print(1)
 9         self.a1=a1
10         self.a2=a2
11         print(666)
12     def __new__(cls, *args, **kwargs):
13         print(2)
14         return object.__new__(cls)  #python内部创建一个空的对象  没有return就不能执行__init__
15 obj=Foo(1,2)
8.真正的构造方法
 1 class Foo(object):
 2     def __init__(self,a1,a2):
 3         '''
 4         为对象进行数据的初始化
 5         :param a1:
 6         :param a2:
 7         '''
 8         print(1)
 9         self.a1=a1
10         self.a2=a2
11         print(666)
12     def __new__(cls, *args, **kwargs):
13         print(2)
14         v1= object.__new__(cls)  #python内部创建一个空的对象  没有return就不能执行__init__
15         print(v1)  #<__main__.Foo object at 0x0000023912498D30>
16         return v1
17 obj=Foo(1,2)
18 print(obj)  #<__main__.Foo object at 0x0000023912498D30>
19 v1和obj的内存地址是一样的,但内部的数据是不一样的,v1是空的,obj中含有数据
构造方法的补充
所有的类都继承object
posted @ 2018-08-29 15:59  ★行者尚★  阅读(220)  评论(0编辑  收藏  举报