Created
May 8, 2012 07:41
-
-
Save stingh711/2633318 to your computer and use it in GitHub Desktop.
Metaclass test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyMeta(type): | |
def __init__(cls, name, bases, attrs): | |
print "__init__ is called" | |
type.__init__(cls, name, bases, attrs) | |
def __call__(cls, *args, **kargs): | |
print "I am called" | |
return type.__call__(cls, *args, **kargs) | |
def __setattr__(cls, name, value): | |
print "__setattr__ is called" | |
type.__setattr__(cls, name, value) | |
class MyClass(object): | |
__metaclass__ = MyMeta | |
def __init__(self): | |
pass | |
def __setattr__(self, name, value): | |
print "Myclass.__setattr__ is called" | |
super(MyClass, self).__setattr__(name, value) | |
if __name__ == '__main__': | |
c = MyClass() | |
MyClass.a = 1 | |
c.s = 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment