Thursday, April 14, 2016

Python anonymous class name with anonymous class variable name

In Python it is possible to create new type object without any name (or more precisely with an empty name).
Moreover, you can create an anonymous (empty) class variable.
For example to create Phantom class with anonymous (empty) class variable:
>>> Phantom = type('', (object,), {'': 'surprise'})
>>> p = Phantom()
>>> Phantom.__name__
''
>>> p.__class__
<class '__main__.'>
>>> getattr(p, '')
'surprise'

No comments:

Post a Comment