1 组合补充
<1>类或对象可以做字典的key
<2>对象中到底有什么
class Foo(object): # # def __init__(self,age): # self.age = age # # def display(self): # print(self.age) # # data_list = [Foo(8),Foo(9)] # for item in data_list: # print(item.age,item.display()) #8 8 None 9 9 None
#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') # # # 创建了一个列表,列表中有三个对象(实例) # # [ StarkConfig对象(num=1), StarkConfig对象(num=2), RoleConfig对象(num=3) ] # config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)] # for item in config_obj_list: # print(item.num) #结果为 1 2 3
2主动调用其它类成员(2种方式):
方式一:类.实例方法(自己传self) 与继承无关
class Base: def f1(self): print('五个功能') class Foo: def f1(self): print('三个功能') Base.f1(self) #主动调用Base里面的f1,传了self obj =Foo() obj.f1()
方式二:按照类的继承顺序,找下一个
class Base(object): def f1(self): print('五个功能') class Foo(Base): def f1(self): super().f1() #调用继承顺序中的下一个 print('三个功能') obj = Foo() obj.f1()
class Foo(object): def f1(self): super().f1() print('三个功能') class Bar(object): def f1(self): print('6各功能') class Info(Foo,Bar): pass # obj =Foo() # obj.f1()# error'super' object has no attribute 'f1' 创建对象是Foo obj = Info() obj.f1() #结果为 6各功能 三个功能
3特殊成员:
A 类名() 自动执行__init__
# obj = Foo(1,2)
class Foo: def __init__(self,name,age): self.name=name self.age=age obj=Foo(1,2) #创建对象时就执行init初始化 1,2的传到self.name self.age中
B 对象() 自动执行__call__
# ret = obj(1,2,3,k=456)
class Foo: def __call__(self, *args, **kwargs): print('hahah',123,args,kwargs)#打印 hahah 123 (1, 2, 3) return 666 #可返回值 obj=Foo() ret=obj(1,2,3,k=456)# 传参,传到位置参数*args中,关键字参数到**kwargs中 print(ret) #返回值666
C对象[ ] 自动执行__getitem__
# ret=obj['鱼香肉丝']
#print(ret)
class Foo: def __getitem__(self, item): print(item) #打印 鱼香肉丝 return 888 obj = Foo() ret=obj['鱼香肉丝'] #自动运行__getitem__方法 print(ret) #返回值888
D 对象['xx']=11 自动执行__setitem__
# obj['k1']=123
class Foo: def __setitem__(self, key, value): print(key,value) #打印 k1 123 obj = Foo() obj['k1']=123 #自动运行__setitem__方法
E del 对象[xx] 自动执行__delitem__
# del obj ['uuu']
class Foo: def __delitem__(self, key): print(key) #打印 k1 obj = Foo() del obj['k1'] #自动运行__delitem__方法
F 对象+对象 自动执行__add__
# obj1=Foo(1,2)
#obj2=Foo(88,99)
# ret =obj2+obj1
#print(ret)
class Foo(object): def __init__(self,a1,a2): #必须有 self.a1=a1 self.a2=a2 def __add__(self, other): return self.a1+other.a2 obj= Foo(1,2) obj1 = Foo(88,99) ret = obj+obj1 #自动运行__add__方法 print(ret)
G with 对象 自动执行__enter__/__exit__
#obj=Foo(1,2)
#with obj as f:
# print(f)
class Foo(object): def __init__(self, a1, a2): self.a1=a1 self.a2=a2 def __enter__(self): print(888) return 999 def __exit__(self, exc_type, exc_val, exc_tb): print(666) obj= Foo(1,2) with obj as f: print(f)
H真正的构造方法:
# 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)
#
# obj2 = Foo(11,12)
# print(obj2)