Skip to content

Instantly share code, notes, and snippets.

@minichate
Created September 27, 2011 15:58
Show Gist options
  • Save minichate/1245471 to your computer and use it in GitHub Desktop.
Save minichate/1245471 to your computer and use it in GitHub Desktop.
Metaclasses
class M(type):
def __call__(self, *args, **kwargs):
obj = type.__call__(self, *args)
setattr(obj, "foo", self.baz)
setattr(obj, "long_other_name", self.baz)
setattr(obj, "metatastic", True)
return obj
def baz(cls):
return "FooBaz -- Base class %s" % cls
class B:
__metaclass__ = M
def bar(self):
return "Bar"
b = B()
print b.bar()
print b.foo()
print b.long_other_name()
print b.metatastic
Bar
FooBaz -- Base class <class '__main__.B'>
FooBaz -- Base class <class '__main__.B'>
True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment