Skip to content

Instantly share code, notes, and snippets.

@stingh711
Created May 8, 2012 07:41
Show Gist options
  • Save stingh711/2633318 to your computer and use it in GitHub Desktop.
Save stingh711/2633318 to your computer and use it in GitHub Desktop.
Metaclass test
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