一、组合补充
1.类或对象是否能做字典中的key
class Foo: pass dic = { Foo:1, Foo():2 } print(dic) #{<class '__main__.Foo'>: 1, <__main__.Foo object at 0x000001BD743A8BA8>: 2} #很明显是可以做字典的key的
2.对象中到底有什么(*)
class Foo: def __init__(self,age): self.age =age def dispaly(self): print(self.age) data_list = [Foo(8),Foo(9)] for el in data_list: print(el.age,el.dispaly()) # 8 这个8是在print语句中的el.display()执行的结果, # 8 None 这个8是print语句中el.age的值,None是el.display()没有返回值 # 9 # 9 None
3.
class StarkConfig(object): def __init__(self,num): self.num = num def changelist(self,request): print(self.num,request) class RoleConfig(StarkConfig): def changelist(self,request): print('666') config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)] for item in config_obj_list: print(item.num) #item.num其实就是StarkConfig(1).num # 1 # 2 # 3
4.
class StarkConfig(object): def __init__(self,num): self.num = num def changelist(self,request): print(self.num,request) class RoleConfig(StarkConfig): pass config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)] for item in config_obj_list: item.changelist(168) # 1 168 # 2 168 # 3 168
5.
class StarkConfig(object): def __init__(self,num): self.num = num def changelist(self,request): print(self.num,request) class RoleConfig(StarkConfig): def changelist(self,request): print(666,self.num) config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)] for item in config_obj_list: item.changelist(168) # 1 168 # 2 168 # 666 3
6.
class StarkConfig(object): def __init__(self,num): self.num = num def changelist(self,request): print(self.num,request) def run(self): self.changelist(999) class RoleConfig(StarkConfig): def changelist(self,request): print(666,self.num) config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)] config_obj_list[1].run() config_obj_list[2].run() # 2 999 # 666 3
7.
class StarkConfig(object): def __init__(self,num): self.num = num def changelist(self,request): print(self.num,request) def run(self): self.changelist(999) class RoleConfig(StarkConfig): def changelist(self,request): print(666,self.num) class AdminSite(object): def __init__(self): self._registry = {} def register(self,k,v): self._registry[k] = v site = AdminSite() print(len(site._registry)) #0 site.register('range',666) site.register('shilei',438) print(len(site._registry)) #2 site.register('lyd',StarkConfig(19)) site.register('yjl',StarkConfig(20)) site.register('fgz',RoleConfig(33)) print(len(site._registry)) #5
8.
class StarkConfig(object): def __init__(self,num): self.num = num def changelist(self,request): print(self.num,request) def run(self): self.changelist(999) class RoleConfig(StarkConfig): def changelist(self,request): print(666,self.num) class AdminSite(object): def __init__(self): self._registry = {} def register(self,k,v): self._registry[k] = v site = AdminSite() site.register('lyd',StarkConfig(19)) site.register('yjl',StarkConfig(20)) site.register('fgz',RoleConfig(33)) print(len(site._registry)) # 3 for k,row in site._registry.items(): row.changelist(5) # 19 5 # 20 5 # 666 33
9.
class StarkConfig(object): def __init__(self,num): self.num = num def changelist(self,request): print(self.num,request) def run(self): self.changelist(999) class RoleConfig(StarkConfig): def changelist(self,request): print(666,self.num) class AdminSite(object): def __init__(self): self._registry = {} def register(self,k,v): self._registry[k] = v site = AdminSite() site.register('lyd',StarkConfig(19)) site.register('yjl',StarkConfig(20)) site.register('fgz',RoleConfig(33)) print(len(site._registry)) # 3 for k,row in site._registry.items(): row.run() # 19 999 # 20 999 # 666 33
10.
class UserInfo(object): pass class Department(object): pass class StarkConfig(object): def __init__(self,num): self.num = num def changelist(self,request): print(self.num,request) def run(self): self.changelist(999) class RoleConfig(StarkConfig): def changelist(self,request): print(666,self.num) class AdminSite(object): def __init__(self): self._registry = {} def register(self,k,v): self._registry[k] = v(k) site = AdminSite() site.register(UserInfo,StarkConfig) site.register(Department,StarkConfig) for k,row in site._registry.items(): row.run() # <class '__main__.UserInfo'> 999 # <class '__main__.Department'> 999
总结:
1.要知道对象中封装了什么
2.self到底是谁
二、主动调用其他类的成员
class Base(object): def f1(self): print("asd") obj = Base() #我们在调用类中的方法时,一般都是先创建一个对象,然后用对象名.方法名就可以调用 obj.f1() obj = Base() Base.f1(obj) #用类名也可以调用类中的方法,但是方法中的self必须手动传入
方式一:
class Base(object): def f1(self): print("asd") class Foo(Base): def f1(self): print("qwe") Base.f1(self) obj = Foo() obj.f1() # qwe # asd
方式二、 按照类的继承顺序,找下一个
class Base(object): def f1(self): print("asd") class Foo(Base): def f1(self): print("qwe") super().f1() obj = Foo() obj.f1() # qwe # asd
class Bar(object): def f1(self): print("asd") class Foo(object): def f1(self): print("qwe") super().f1() #这里的self是App,按照App继承顺序的下一个是Bar所以调用了Bar中的f1方法 class App(Foo,Bar): pass obj = App() obj.f1() # qwe # asd
三、特殊成员
class Foo(object): def __init__(self,a1,a2): self.a1 = a1 self.a2 = a2 def __call__(self, *args, **kwargs): print(111,args,kwargs) return 123 def __getitem__(self, item): print(item) return 11 def __setitem__(self, key, value): #没有返回值 print(key,value,1111) def __delitem__(self, key): #没有返回值 print(key) def __add__(self, other): print(other) return self.a1 + other.a2 def __enter__(self): print(111) return 999 def __exit__(self, exc_type, exc_val, exc_tb): return 222 #1 类名() 自动执行__init__ # obj = Foo(1,2) #2 对象() 自动执行__call__ # obj = Foo(1,2) # ret = obj(1,2,3,4,k1="123") #111 (1, 2, 3, 4) {'k1': '123'} # print(ret) #123 #3 对象["xx"] 自动执行__getitem__ # obj = Foo(1,2) # ret = obj["aa"] #aa # print(ret) #11 #4 对象["xx"]=xx 自动执行__setitem__ # obj = Foo(1,2) # obj["k1"] = "v1" #k1 v1 1111 #5 del 对象["xx"] 自动执行__delitem__ # obj = Foo(1,2) # del obj["k1"] #k1 #6 对象+对象 自动执行__add__ # obj1 = Foo(1,2) # obj2 = Foo(88,99) # ret = obj1+obj2 #<__main__.Foo object at 0x0000020F3B08E400> # print(ret) #100 #7 with 对象 自动执行__enter__/__exit__ # obj = Foo(1,2) # with obj as f: # print(f) # # 111 # # 999 # 8 真正的构造方法 class Foo(object): def __init__(self, a1, a2): # 初始化方法 """ 为空对象进行数据初始化 :param a1: :param a2: """ self.a1 = a1 self.a2 = a2 def __new__(cls, *args, **kwargs): # 构造方法 """ 创建一个空对象 :param args: :param kwargs: :return: """ return object.__new__(cls) # Python内部创建一个当前类的对象(初创时内部是空的.). obj1 = Foo(1,2) print(obj1) #<__main__.Foo object at 0x0000024D96198DD8> obj2 = Foo(11,12) print(obj2) #<__main__.Foo object at 0x0000024D96198DA0>