zoukankan      html  css  js  c++  java
  • Mateclass

    Mateclass

    一切皆对象:

    Eg:

    class Foo:

    pass

    f=Foo()

    In [60]: print(type(f))

    <class '__main__.Foo'>

    In [61]: print(type(Foo))

    <class 'type'>

    即: f是Foo的对象 Foo是type的对象

    那么我们现在不用type作为Foo的类:

    1. # coding=utf-8  
    2. class Mytype(type):  
    3.     def __init__(cls, what, bases=None, dict=None):  
    4.         super().__init__(what, bases, dict)  
    5.         print("Mytype_init_")  
    6.     
    7.     def __call__(self, *args, **kwargs):
    8.          return super().__call__(*args, **kwargs)
    9.     
    10.     
    11. class Foo(metaclass=Mytype):  
    12.     def __init__(self):  
    13.         super().__init__()  
    14.         print("Foo__init__")  
    15.    
    16.     @staticmethod  
    17.     def __new__(cls, *more):  
    18.         print("Foo__new__")  
    19.         # return super().__new__(cls, *more)  

    解释器从上到下执行,9行之前,先由type创建Mytype.到第11,创建Foo对象,type__call__方法调用Mytype__new__方法,再是__init__方法;

    这时候执行,可以看到的运行结果为: Mytype_init_

    下面继续:

    1. # coding=utf-8  
    2. class Mytype(type):  
    3.     def __init__(cls, what, bases=None, dict=None):  
    4.         super().__init__(what, bases, dict)  
    5.         print("Mytype_init_")  
    6.     
    7.     def __call__(self, *args, **kwargs):  
    8.         print("Mytype____call__")  
    9.         print(self.__name__)  #Foo
    10.         print(self)  #<class '__main__.Foo'>
    11.         # return super().__call__(*args, **kwargs)  
    12.     
    13.     
    14.     
    15. class Foo(metaclass=Mytype):  
    16.     def __init__(self):  
    17.         super().__init__()  
    18.         print("Foo__init__")  
    19.    
    20.     @staticmethod  
    21.     def __new__(cls, *more):  
    22.         print("Foo__new__")  
    23.         # return super().__new__(cls, *more)  
    24.     
    25. f=Foo() 

    可以看到在25行的时候后面加括号,调用了Foo对象的__call__方法,这时候self就是Foo,在这里调用self(Foo)的__new__方法,

    接下来

    1. # coding=utf-8  
    2. class Mytype(type):  
    3.     def __init__(cls, what, bases=None, dict=None):  
    4.         super().__init__(what, bases, dict)  
    5.         print("Mytype_init_")  
    6.     
    7.     def __call__(self, *args, **kwargs):  
    8.         print("Mytype____call__")  
    9.         return super().__call__(*args, **kwargs)  
    10.     
    11.     
    12.     
    13. class Foo(metaclass=Mytype):  
    14.     def __init__(self):  
    15.         super().__init__()  
    16.         print("Foo__init__")  
    17.    
    18.     @staticmethod  
    19.     def __new__(cls, *more):  
    20.         print("Foo__new__")  
    21.         print(super().__new__(cls, *more))   #<__main__.Foo object at 0x01F2BAD0>
    22.         return super().__new__(cls, *more)  
    23.     
    24. f=Foo()  
    25.     
    26. # Mytype_init_  
    27. # Mytype____call__  
    28. # Foo__new__  
    29. # <__main__.Foo object at 0x01F2BAD0>  
    30. # Foo__init__  
    31. #  
    32. # Process finished with exit code  0  

    So 当我们调用父类的方法时,顺带打印下我们关系的运行关系:

    可以看到,object对象是在__new__方法中生成的.

    总结:实例化一个类: 先在定义类的时候,解释器已经对其进行解释,type类执行,实例化的时候type类的__call__方法被调用,type.__call__方法将调用该类的__new__方法.此时对象生成,然后该类的__init__方法被调用.

    即:实例化一个类,他的__new__方法先被执行,并在此时创建对象,然后,__init__方法被执行

  • 相关阅读:
    前进篇
    2014年12月14日记
    转载了两篇别人写的话语
    想好了,也决定了
    活着
    c#字典排序
    插值转向
    unity手游使用terrian注意事项
    委托delegate 泛型委托action<> 返回值泛型委托Func<> 匿名方法 lambda表达式 的理解
    推荐博客关于uniy
  • 原文地址:https://www.cnblogs.com/twotigers/p/7775134.html
Copyright © 2011-2022 走看看