組合(巢狀)的 補充,主動呼叫其他成員 以及特殊成員

Bound_w發表於2018-08-30

組合的補充

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)

config_obj_list = [StarkConfig(1),StarkConfig(2),RoleConfig(3)]
config_obj_list[1].run()   #2 999
config_obj_list[2].run() # 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()
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

 

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

 

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()
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)) # 3
for k,row in site._registry.items():
    row.run()


<class '__main__.UserInfo'> 999
<class '__main__.Department'> 999

 

主動呼叫其他類的成員

1.方法一

class foo(object):
    def f1(self):
        print("你好啊")
class user(object):
    def f1(self):
        print("hello")
        foo.f1(self)
obj=user()
obj.f1()

呼叫結果:
hello
你好啊
注意:主動呼叫的時候需要自己手動傳self

 

2.方法二:按照類的繼承順序,找下一個

class foo(object):
    def f1(self):
        print("你好啊")
class user(foo):
    def f1(self):
        print("hello")
        super().f1()#按照繼承順序找下一個
obj=user()
obj.f1()

hello
你好啊

 特殊成員:

1.類名()自動執行__init__

obj=foo(1,2)

2.物件() 自動執行__call__

class fool(object):
    def __call__(self, *args, **kwargs):
        print(22,33,44,55)
        return 111
obj=fool
ret=obj.__call__(1)
print(ret)


22 33 44 55
111#返回值

 

 3.物件["xx"]=11 自動執行__setitem__沒有返回值

 

class foo(object):
    def __setitem__(self, key, value):
        print("hhh")
obj=foo()
obj.__setitem__(1,0)

hhh

 

 4.物件["xx"] 會自動執行__getitem__有返回值

 

class foo(object): 
    def __getitem__(self, item):
        print("123")
        return "bbb"
obj=foo()
ret=obj.__getitem__(0)
print(ret)

123
bbb

 

 5.del 物件[xx]   自動執行__delitem__     有返回值

class foo(object): 
    def __delitem__(self, key):
        print(555)
        return 222

obj=foo()
ret=obj.__delitem__("key")
print(ret)



555
222

 

6.物件+物件  會自動執行__add__

 class foo(object):
    def __add__(self, other):
        return self.a1+other.a2

 

7.with 物件 自動執行__enter__ /__exit__


def __exit__(self, exc_type, exc_val, exc_tb)

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)

obj2 = Foo(11,12)
print(obj2)

 

相關文章