Create a gist now

Instantly share code, notes, and snippets.

Python namespaces with side effects
>>> class Madness(dict):
... def __setitem__(self, attr, value):
... if isinstance(value, type):
... value.__name__ = attr
... dict.__setitem__(self, attr, value)
...
>>> code = """\
... class Example(object): pass
... NewName = Example
... print(Example.__name__)
... """
>>> exec code in Madness()
NewName
>>> class Madness(dict):
... def __setitem__(self, attr, value):
... if isinstance(value, type):
... value.__name__ = attr
... dict.__setitem__(self, attr, value)
...
>>> code = """\
... class Example: pass
... NewName = Example
... print(Example.__name__)
... """
>>> exec(code, Madness())
NewName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment