You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Phillip B Oldham <ph...@gmail.com> on 2009/09/09 16:23:14 UTC

Generated object inheritance (Python)

Is there a particular reason why, in Python, the objects that
represent enums don't inherit from a parent EnumObject type?

It would be useful if they, and the other objects, inherited from base
objects - specifically, `isinstance` could be used for testing:
>>> isinstance(MyEnum, EnumObject)
True
>>> isinstance(MyStruct, StructObject)
True

etc.

This would allow for monkey-patching the "base" object with some handy
class methods and having them be inherited by the child classes.
Here's a trivial example:

>>> def key_from_val(cls, val):
...   for n, v in cls.__dict__.items():
...     if n.isupper() and v is val:
...       return n
...
>>> EnumObject.key_from_val = classmethod(key_from_val)
>>> MyEnumObject.key_from_val(1)
'FIRST_ENUM_ITEM'

-- 
Phillip B Oldham
phillip.oldham@gmail.com
+44 (0) 7525 01 09 01