TypeError: Cannot extend enumerations 目前无法创建包含成员的基本枚举类并将其用于其他枚举类(如上例所示)。还有其他方法可以实现 Python 枚举的继承吗? 虽然不常见,但有时从多个模块创建一个枚举很有用。aenum1库通过extend_enum函数支持这一点: from aenum import Enum, extend_enum class Index(Enum): Device...
TypeError: Cannot extend enumerations >>> class Foo(Enum): ... def some_behavior(self): ... pass ... >>> class Bar(Foo): ... HAPPY = 1 ... SAD = 2 ... 不能这么做的原因是可能破坏某些重要的不允许改变的值(原话是would lead to a violation of some important invariants of bytes ...
>>> class MoreColor(Color): ... PINK = 17...Traceback (most recent call last):...TypeError: MoreColor: cannot extend enumeration 'Color' AI代码助手复制代码 但父类没有枚举成员,仅仅定义了函数是可以的: classFoo(Enum):defsome_behavior(self): passclassBar(Foo):HAPPY=1SAD=2 AI代码助手复...
TypeError: Cannot extend enumerations 这是由于Mood已定义了枚举成员,不允许扩展。
("Cannot extend enumerations")# base is now the last base in basesifnotissubclass(base, Enum):raiseTypeError("new enumerations must be created as ""`ClassName([mixin_type,] enum_type)`")# get correct mix-in type (either mix-in type of Enum subclass, or# first base if last base is...
TypeError: Cannot extend enumerations 但是允许这样的写法: >>> >>> class Foo(Enum): ... def some_behavior(self): ... pass ... >>> class Bar(Foo): ... HAPPY = 1 ... SAD = 2 ... 允许子类化定义了成员的枚举将会导致违反类型与实例的某些重要的不可变规则。 在另一方面,允许...
To compare a string with an enum, extend from thestrclass when declaring your enumeration class, e.g.class Color(str, Enum):. You will then be able to compare a string to an enum member using the equality operator==. How to compare a string with an Enum in Python | bobbyhadz ...
enum_auto enum_compare enum_create enum_extend enum_function enum_iterate enum_order enum_unique
@enum.unique: 枚举专用的类装饰器。它在枚举的__members__属性中只要查找到成员别名就抛出ValueError异常 代码语言:javascript 代码运行次数:0 运行 AI代码解释 >>>fromenumimportEnum,unique>>>@unique...classMistake(Enum):...ONE=1...TWO=2...THREE=3...FOUR=3...Traceback(most recent call last):...
定义枚举的子类 定义枚举的子类需要满足:父枚举没有定义任何的枚举成员,此时才允许有子类。 >>> class MoreColor(Color): ... pink = 17 ... TypeError: Cannot extend enumerations 1. 2. 3. 4. 这是由于Mood已定义了枚举成员,不允许扩展。