zoukankan      html  css  js  c++  java
  • 组合补充,主动调用其他类的成员,特殊成员

    一丶组合补充

    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.__str__如果类中定义了__str__方法,那么打印对象时,默认该输出方法的返回值

    class Foo:
    
        def __str__(self):
            return 1
    
    
    obj = Foo()
    print obj
    # 输出:1

    9.__doc__  表示类的描述信息

    class Foo:
        """ 描述类信息 """
    
        def func(self):
            pass
    
    print Foo.__doc__
    #输出:类的描述信息

    10.__dict__  类或者对象的所有成员

    class Foo(object):
        def __init__(self,name,age):
            self.name = name
            self.age = age
    
        def func(self):
            pass
    
    obj1 = Foo('刘博文',99)
    obj2 = Foo('史雷',89)
    
    print(obj1.__dict__) # {'name': '刘博文', 'age': 99}
    print(obj2.__dict__) # {'name': '史雷', 'age': 89}

    11.__iter__用于迭代器,之所以列表、字典、元组可以进行for循环,是因为类型内部定义了 __iter__ 

        # l1是list类的一个对象,可迭代对象
                l1 = [11,22,33,44]
    
                # l2是list类的一个对象,可迭代对象
                l2 = [1,22,3,44]
    
    
                class Foo(object):
                    def __init__(self,name,age):
                        self.name = name
                        self.age = age
    
                    def func(self):
                        pass
    
                    def __iter__(self):
                        # return iter([11,22,33,44,55,66])
    
                        yield 11
                        yield 22
                        yield 33
    
                # obj1是Foo类的一个对象,可迭代对象
                """
                如果想要把不可迭代对象 -> 可迭代对象
                1. 在类中定义__iter__方法
                2. iter内部返回一个迭代器(生成器也是一种特殊迭代器)
                """
                obj1 = Foo('刘博文',99)
    
                for item in obj1:
                    print(item)

    12.真正的构造方法

    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)
    构造方法
  • 相关阅读:
    vs2015编译各种库
    回测分析软件
    交易平台
    策略框架
    期货homes平台以及仿ctp接口
    k线中转器
    期货数据服务器以及取数据接口
    c++ 二叉树的遍历(迭代,递归)
    c++实现单向链表的一些操作
    numpy操作
  • 原文地址:https://www.cnblogs.com/xintiao-/p/9554835.html
Copyright © 2011-2022 走看看