标签:print new 构造 面向 png __init__ 方法 dmi rgs
class Foo: pass user_info = { Foo:1, Foo():5 } print(user_info) #{<class ‘__main__.Foo‘>: 1, <__main__.Foo object at 0x000002CF8B1A9CF8>: 5}
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())
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
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) # 创建了一个列表,列表中有三个对象(实例) # [ StarkConfig对象(num=1), StarkConfig对象(num=2), RoleConfig对象(num=3) ] config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)] for item in config_obj_list: item.changelist(168)
#1 168
#2 168
#666 3
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)
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) print(len(site._registry)) for k,row in site._registry.items(): row.run()
class Base(object): def f1(self): print(‘5个功能‘) class Foo(object): def f1(self): print(‘3个功能‘) Base.f1(self) obj = Foo() obj.f1()
解析:当调用类Foo中的f1方法时,f1方法中也存在着调用类Base中f1的语句,所以调用类Foo中的f1也相当于调用了类Base
方式二丶按照类的继承顺序,找下一个.
class Foo(object): def f1(self): super().f1() print(‘3个功能‘) class Bar(object): def f1(self): print(‘6个功能‘) class Info(Foo,Bar): pass obj = Info() obj.f1()
class Foo: def __init__(self, name): self.name = name self.age = 18 obj = Foo(‘迪迦‘) # 自动执行类中的 __init__ 方法
class Foo: def __init__(self): pass def __call__(self, *args, **kwargs): print ‘__call__‘ obj = Foo() # 执行 __init__ obj() # 执行 __call__
class Foo(object): def __getitem__(self, item): print(item) return 8 obj = Foo() ret = obj[‘yu‘] print(ret)
class Foo(object): def __setitem__(self, key, value): print(key, value, 111111111) obj = Foo() obj[‘k1‘] = 123
class Foo(object): def __delitem__(self, key): print(key) obj = Foo() del obj[‘uuu‘]
class Foo(object): def __init__(self, a1, a2): self.a1 = a1 self.a2 = a2 def __add__(self, other): return self.a1 + other.a2 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 __enter__(self): print(‘1111‘) return 999 def __exit__(self, exc_type, exc_val, exc_tb): print(‘22222‘) obj = Foo(1,2) with obj as f: print(f) print(‘内部代码‘)
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)
标签:print new 构造 面向 png __init__ 方法 dmi rgs
原文地址:https://www.cnblogs.com/qicun/p/9555093.html