[Python-ideas] constant/enum type in stdlib

drekin at gmail.com drekin at gmail.com
Thu Jan 31 11:38:29 CET 2013


Hello. It should be also possible to specify the values of enum constants explicitly. For 'bitmask' type only powers of 2 should be allowed or maybe the values could be the exponents (as your TypeFlag example indicates).

The same way 'symbolic' type acts as str and 'sequential' type acts as int, 'bitmask' type could act both as int and set (or frozenset) since its semantics is like of set. The enum value object could represent both the int value and corresponding singleton set. OR-ing would produce corresponding multivalue set.

>>> isinstance(TypeFlag.HEAPTYPE, int)
True
>>> isinstance(TypeFlag.HEAPTYTE, set)
True

>>> TypeFlag.HAS_GC | TypeFlag.INT_SUBCLASS
TypeFlag.HEAPTYPE|HAS_GS # or maybe <TypeFlag {HEAPTYPE, HAS_GS}>
>>> TypeFlag.HEAPTYPE in (TypeFlag.HEAPTYPE | TypeFlag.HAS_GC)
True
>>> TypeFlag.HEAPTYPE in TypeFlag.HEAPTYPE
True

>>> TypeFlag(1)
TypeFlag.HEAPTYPE
>>> TypeFlag(2)
TypeFlag.HAS_GC
>>> set(TypeFlag.HEAPTYPE)
{1}
>>> set(TypeFlag.HEAPTYPE | TypeFlag.HAS_GC)
{1, 2}
>>> int(TypeFlag.HEAPTYPE)
2
>>> int(TypeFlag.HEAPTYPE | TypeFlag.HAS_GC)
6

Note the difference between n and 2 ** n semantics. So there slould be something like
>>> TypeFlag.decompose(2)
TypeFlag.HEAPTYPE
>>> TypeFlag.decompose(6)
TypeFlag.HEAPTYPE|HAS_GS


Regards, Drekin




More information about the Python-ideas mailing list