关于组合(嵌套)的补充和实例

一、对象和类可以做字典的key

1 class Foo:
2     pass
3 
4 user_info = {
5     Foo:1,   #类做key
6     Foo():5  # 对象做key
7 }
8 
9 print(user_info)

二、把对象作为列表里的元素
1、

 1 class Foo(object):
 2 
 3      def __init__(self,age):
 4          self.age = age
 5 
 6      def display(self):
 7          print(self.age)
 8 
 9 data_list = [Foo(8),Foo(9)]
10 for item in data_list:
11      print(item.age,item.display())

2、

 1 class StarkConfig(object):
 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 # # 创建了一个列表,列表中有三个对象(实例)
15 # # [ StarkConfig对象(num=1), StarkConfig对象(num=2), RoleConfig对象(num=3) ]
16 config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)]
17 for item in config_obj_list:
18      print(item.num)
19 
20  1 2 3

4、

 1 class StarkConfig(object):
 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      pass
11 
12 # # 创建了一个列表,列表中有三个对象(实例)
13 # # [ StarkConfig对象(num=1), StarkConfig对象(num=2), RoleConfig对象(num=3) ]
14 config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)]
15 for item in config_obj_list:
16      item.changelist(168)
 # 1 168、2 168、3 168

5、

 1 class StarkConfig(object):
 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 # # 创建了一个列表,列表中有三个对象(实例)
15 # # [ StarkConfig对象(num=1), StarkConfig对象(num=2), RoleConfig对象(num=3) ]
16 config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)]
17 for item in config_obj_list:
18      item.changelist(168)
19 
20 # 1 168、2 168、666 3

6、

 1  class StarkConfig(object):
 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  config_obj_list[1].run() # 2 999
19  config_obj_list[2].run() # 666 3

7、

 1  class StarkConfig(object):
 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 
18  class AdminSite(object):
19      def __init__(self):
20          self._registry = {}
21 
22      def register(self,k,v):
23          self._registry[k] = v
24 
25  site = AdminSite()
26  print(len(site._registry)) # 0
27  site.register('range',666)
28  site.register('shilei',438)
29  print(len(site._registry)) # 2
30 
31  site.register('lyd',StarkConfig(19))
32  site.register('yjl',StarkConfig(20))
33  site.register('fgz',RoleConfig(33))
34 
35  print(len(site._registry)) # 5

8、

 1 (object):
 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(object):
18     def __init__(self):
19         self._registry = {}
20 
21     def register(self,k,v):
22         self._registry[k] = v
23 
24 site = AdminSite()
25 site.register('lyd',StarkConfig(19))
26 site.register('yjl',StarkConfig(20))
27 site.register('fgz',RoleConfig(33))
28 print(len(site._registry)) # 3
29 
30 for k,row in site._registry.items():
31     row.changelist(5)
 # 19 5、20 5、666 33

9、

 1 class StarkConfig(object):
 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(object):
18     def __init__(self):
19         self._registry = {}
20 
21     def register(self,k,v):
22         self._registry[k] = v
23 
24 site = AdminSite()
25 site.register('lyd',StarkConfig(19))
26 site.register('yjl',StarkConfig(20))
27 site.register('fgz',RoleConfig(33))
28 print(len(site._registry)) # 3
29 
30 for k,row in site._registry.items():
31     row.run()
 # 19 999、20 999、 666 33

10、

 1 class UserInfo(object):
 2     pass
 3 
 4 class Department(object):
 5     pass
 6 
 7 class StarkConfig(object):
 8 
 9     def __init__(self,num):
10         self.num = num
11 
12     def changelist(self,request):
13         print(self.num,request)
14 
15     def run(self):
16         self.changelist(999)
17 
18 class RoleConfig(StarkConfig):
19 
20     def changelist(self,request):
21         print(666,self.num)
22 
23 class AdminSite(object):
24     def __init__(self):
25         self._registry = {}
26 
27     def register(self,k,v):
28         self._registry[k] = v(k)
29 
30 site = AdminSite()
31 site.register(UserInfo,StarkConfig)
32 site.register(Department,StarkConfig)
33 print(len(site._registry)) # 3
34 for k,row in site._registry.items():
35     row.run()
# <class '__main__.UserInfo'> 999
# <class '__main__.Department'> 999

 

posted @ 2018-08-29 15:41  神神气气  阅读(231)  评论(0编辑  收藏  举报