MetaClass

type

class Hello(object) <==> type('Hello', (object, ), dict())

  • class

    class Hello(object):
        def __init__(self):
            self.meta = 110
            print self.foo
            print self.bar()
    
        @property
        def foo(self):
            return self.meta
    
        def bar(self, plus=1):
            return self.meta + 1
    
    hello = Hello()
    # 110
    # 111
    
  • type

    def __init__(self):
        self.meta = 110
        print self.foo
        print self.bar()
    
    @property
    def foo(self):
        return self.meta
    
    def bar(self, plus=1):
        return self.meta + 1
    
    Hello = type('Hello', (object, ), dict(__init__=__init__, foo=foo, bar=bar))
    
    hello = Hello()
    # 110
    # 111
    

metaclass

References

[1] Ivan Smirnov's Blog, Python metaclasses

results matching ""

    No results matching ""