1. 程式人生 > >組合補充,主動調用其他類的成員,特殊成員

組合補充,主動調用其他類的成員,特殊成員

items isp register src ddb img with turn sed

一丶組合補充

1.類或對象是否能做字典的key

技術分享圖片
1 class Foo:
2     pass
3 
4 user_info = {
5     Foo:1,
6     Foo():5
7 }
8 
9 print(user_info)
類做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())
...

3.燒腦練習 top 1

技術分享圖片
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)
...

4.燒腦練習top2

技術分享圖片
class StarkConfig(object):

    def __init__(self,num):
        self.num 
= num def changelist(self,request): print(self.num,request) class RoleConfig(StarkConfig): pass # 創建了一個列表,列表中有三個對象(實例) # [ 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)
...

5.燒腦練習top3

技術分享圖片
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)
...

6.燒腦練習top4

技術分享圖片
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() # 666 3
...

7.燒腦練習top5

技術分享圖片
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.燒腦練習top6

技術分享圖片
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)
...

9.燒腦練習top7

技術分享圖片
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()
...

10.燒腦練習top8

技術分享圖片
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 Base(object):

                def f1(self):
                    print(5個功能)

            class Foo(object):

                def f1(self):
                    print(3個功能)
                    Base.f1(self)

            obj = Foo()
            obj.f1(

 總結:Base.實例方法(自己上傳self)

   與繼承能力無關

    方法二:

            class Foo(object):
                def f1(self):
                    super().f1()
                    print(3個功能)

            class Bar(object):
                def f1(self):
                    print(6個功能)

            class Info(Foo,Bar):
                pass

            # obj = Foo()
            # obj.f1()

            obj = Info()
            obj.f1()

  

三丶特殊成員

1.類名() 自動執行__init__

class Foo(object):

    def __init__(self,a1,a2):
        self.a1 = a1
        self.a2 = a2
obj = Foo(1,2)

2.對象() 自動執行__call__

def __call__(self, *args, **kwargs):
        print(11111,args,kwargs)
        return 123
ret = obj(6,4,2,k1 = 456)

3.對象[‘xx‘] 自動執行__getitem__

def __setitem__(self, key, value):
        print(key,value,111111111)
ret = obj[yu]
print(ret)

4.對象[‘xx‘] = 11 自動執行__setitem__

def __setitem__(self, key, value):
        print(key,value,111111111)
obj[k1] = 123

5.del 對象[xx] 自動執行__delitem__

 def __delitem__(self, key):
        print(key)
del obj[uuu]

6.對象+對象 自動執行__add__

def __add__(self, other):
      return self.a1 + other.a2
obj1 = Foo(1,2)
obj2 = Foo(88,99)
ret = obj2 + obj1
print(ret)

7.with 對象   自動執行 __enter/__exit__

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(內部代碼)

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)
構造方法

組合補充,主動調用其他類的成員,特殊成員