thrift-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Phillip B Oldham <phillip.old...@gmail.com>
Subject Generated object inheritance (Python)
Date Wed, 09 Sep 2009 14:23:14 GMT
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

Mime
View raw message