Message229006
Sample code:
--------------------------------------------------------------------------------
class AutoEnum(enum.Enum):
"""
Automatically numbers enum members starting from 1.
Includes support for a custom docstring per member.
"""
__last_number__ = 0
def __new__(cls, *args):
"""
Ignores arguments (will be handled in __init__.
"""
value = cls.__last_number__ + 1
cls.__last_number__ = value
obj = object.__new__(cls)
obj._value_ = value
return obj
def __init__(self, *args):
"""
Can handle 0 or 1 argument; more requires a custom __init__.
0 = auto-number w/o docstring
1 = auto-number w/ docstring
2+ = needs custom __init__
"""
if len(args) == 1 and isinstance(args[0], str):
self.__doc__ = args[0]
elif args:
raise TypeError('%s not dealt with -- need custom __init__' % (args,))
class AddressSegment(AutoEnum):
misc = "not currently tracked"
ordinal = "N S E W NE NW SE SW"
secondary = "apt bldg floor etc"
street = "st ave blvd etc"
def huh(self):
return 'did I show up?'
--------------------------------------------------------------------------------
The 'huh' function, shown above, does *not* show up in a dir of either AutoEnum nor AddressSegment, regardless of which class it is defined in -- this is because a directory of an Enum class is meant to show ['__class__', '__doc__', '__members__', '__module__'] + whatever Enum members have been defined.
However, a directory of an Enum member should show any normal methods* available on that member. Currently, if 'huh' is defined in the final class it will show up, but if defined in a superclass it will not.
That is the bug.
* 'normal method' means any method not prefixed by any underscores. |
|
Date |
User |
Action |
Args |
2014-10-10 15:02:22 | ethan.furman | set | recipients:
+ ethan.furman, cool-RR |
2014-10-10 15:02:22 | ethan.furman | set | messageid: <1412953342.09.0.584468622004.issue22506@psf.upfronthosting.co.za> |
2014-10-10 15:02:22 | ethan.furman | link | issue22506 messages |
2014-10-10 15:02:21 | ethan.furman | create | |
|