Last active
August 29, 2015 13:56
-
-
Save jvanasco/9141157 to your computer and use it in GitHub Desktop.
example of collisions in metaclass
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 Meta(type): | |
def __new__(cls, clsname, bases, dct): | |
print "__new__" | |
print cls | |
print clsname | |
print bases | |
print dct | |
attrs = dct.pop('_pairs') | |
for v, k in attrs: | |
setattr(cls, k, v) | |
return type.__new__(cls, clsname, bases, dct) | |
class TestObject(object): | |
__metaclass__ = Meta | |
"""reflects constants in _test_object""" | |
_pairs = ( | |
( 1 , 'A' ), | |
) | |
class OtherObject(object): | |
__metaclass__ = Meta | |
"""reflects constants in _test_object""" | |
_pairs = ( | |
( 1 , 'F' ), | |
( 900 , 'A' ), | |
) | |
print "TestObject" | |
print dir(TestObject) | |
print "~"*80 | |
print "OtherObject" | |
print dir(OtherObject) | |
print "~"*80 | |
print "Meta" | |
print dir(Meta) | |
print TestObject.A | |
print OtherObject.A | |
print TestObject.F | |
print OtherObject.F |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment