zoukankan      html  css  js  c++  java
  • python 创建类先执行metaclass父类__new__ > __init__>__call__ 然后再执行自己的__new__>__init__

    class MyType(type):
    def __init__(self,*args,**kwargs):

    print("Mytype __init__",*args,**kwargs)

    def __call__(self, *args, **kwargs):
    print("Mytype __call__", *args, **kwargs)
    obj = self.__new__(self)
    print("obj ",obj,*args, **kwargs)
    print(self)
    self.__init__(obj,*args, **kwargs)
    return obj

    def __new__(cls, *args, **kwargs):
    print("Mytype __new__",*args,**kwargs)
    return type.__new__(cls, *args, **kwargs)

    print('here...')
    class Foo(object,metaclass=MyType):


    def __init__(self,name):
    self.name = name

    print("Foo __init__")

    def __new__(cls, *args, **kwargs):
    print("Foo __new__",cls, *args, **kwargs)
    return object.__new__(cls)

    f = Foo("Alex")
    print("f",f)
    print("fname",f.name)
  • 相关阅读:
    将博客搬至CSDN
    defender 月考总结
    生日祝福@陈俊翰
    个性签名
    你这是virus吧?
    (CPSCA's)CPOJC+VIJOS
    Sentence by defender
    工作制一览
    最长上升子序列
    mysql约束
  • 原文地址:https://www.cnblogs.com/howhy/p/7838599.html
Copyright © 2011-2022 走看看