Advanced object oriented C (Example metaclass complementary and single mode (to be added))

In some places there is a little ambiguous, first on this, draw a Saturday morning to finish this consolidation

Original 类中

the _init _: generating a control class, after the __new__

_call _: generating a control object

_ New new _ : Control class generates the most fundamental, in fact, the most fundamental nature is not it, is the type of __ Call _, but we do not see the

object.__new__(Person) #生成Person类的对象 空的
type.__new__(cls, name, bases, dic) #生成cls这个类对象,里面有东西


#模板:控制对象的产生
class Mymeta(type):
    def __call__(self, *args, **kwargs):
        obj = object.__new__(self) #先new一个对象
        obj.__init__(*args, **kwargs) #在初始化对象
        return obj
class Person(metaclass=Mymeta):
    def __init__(self,name):
        self.name = name
    def __call__(self, *args, **kwargs):
        print('XXXX')
p = Person('lqz')

Guess you like

Origin www.cnblogs.com/michealjy/p/11469311.html