Created
September 27, 2011 15:58
-
-
Save minichate/1245471 to your computer and use it in GitHub Desktop.
Metaclasses
This file contains hidden or 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 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 |
This file contains hidden or 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
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