[Python-ideas] constant/enum type in stdlib
Ethan Furman
ethan at stoneleaf.us
Wed Jan 30 18:28:35 CET 2013
On 01/30/2013 08:23 AM, Antoine Pitrou wrote:
> If a flexible solution is desired (with either int or str subclassing,
> various numbering schemes), may I suggest another kind of syntax:
>
> class ErrorFlag(Enum):
> type = 'symbolic'
> names = ('strict', 'ignore', 'replace')
>
> class SeekFlag(Enum):
> type = 'sequential'
> names = ('SET', 'CUR', 'END')
>
> class TypeFlag(Enum):
> type = 'bitmask'
> names = ('HEAPTYPE', 'HAS_GC', 'INT_SUBCLASS')
This I like.
>>>> ErrorFlag.ignore
> ErrorFlag.ignore
>>>> ErrorFlag.ignore == 'ignore'
> True
>>>> ErrorFlag('ignore')
> ErrorFlag.ignore
>>>> isinstance(ErrorFlag.ignore, str)
> True
>>>> isinstance(ErrorFlag.ignore, int)
> False
>>>> ErrorFlag(0)
> [...]
> ValueError: invalid value for <class 'enum.ErrorFlag'>: 0
>
>>>> SeekFlag('SET')
> SeekFlag.SET
>>>> SeekFlag('SET') + 0
> 0
>>>> SeekFlag(0)
> SeekFlag.SET
>>>> isinstance(SeekFlag.CUR, int)
> True
>>>> isinstance(SeekFlag.CUR, str)
> False
>
>>>> TypeFlag(1)
> TypeFlag.HEAPTYPE
>>>> TypeFlag(2)
> TypeFlag.HAS_GC
>>>> TypeFlag.HAS_GC | TypeFlag.INT_SUBCLASS
> 6
This should be `TypeFlag.HEAPTYPE|HAS_GC`
+1
~Ethan~
More information about the Python-ideas
mailing list