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__方法被执行

 

转载于:https://www.cnblogs.com/ywhyme/p/7775134.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值