Skip to content

Instantly share code, notes, and snippets.

@jvanasco
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvanasco/9141157 to your computer and use it in GitHub Desktop.
Save jvanasco/9141157 to your computer and use it in GitHub Desktop.
example of collisions in metaclass
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