在__str__上调用__init__会产生aenum.Enum [英] __init__ being called on __str__ result in aenum.Enum
问题描述
在尝试一些愚蠢的事情以使我的 __ str __
方法在Enum成员上正常工作时,我发现了一些我不太理解的行为。我知道以下实现是错误的,但是我不知道它为什么会这样做。
While trying silly things to get my __str__
method working correctly on an Enum member, I discovered some behavior I don't really understand. I'm aware the following implementation is wrong, but I don't know why it does what it does.
请考虑以下类:
from aenum import Enum, AutoValue
class MyEnum(str, Enum, settings=AutoValue, init="value data1 data2"):
__str__ = lambda self: self
def __new__(cls, name, *args, **kwargs):
member = str.__new__(cls)
member._value_ = name
return member
@staticmethod
def _generate_next_value_(name, *args, **kwargs):
return (name,) + args
member1 = 1, 2
member2 = 3, 4
导入时,此功能按预期进行。 MyEnum.member1.data1 == 1
和 MyEnum.member1.data2 == 2
。但是,当我调用 str(MyEnum.member1)
时,会抛出一个异常,说缺少'data2'的值。我在这里跟踪了代码,似乎正在调用 MyEnum.member1 .__ str __().__ init __(MyEnum.member1 .__ str __())
。我不太确定那是通话路径,而是结果。由于 MyEnum .__ init__是Enum .__ init __
并且我们使用的是AutoValue,因此 init 方法需要两个参数。尝试设置 member1.data1 = member1
和 member1.data2
没有值。
On import, this works as exected. MyEnum.member1.data1 == 1
and MyEnum.member1.data2 == 2
. However, when I call str(MyEnum.member1)
an exception is thrown saying "missing value for 'data2'". I traced through the code here and it seems MyEnum.member1.__str__().__init__(MyEnum.member1.__str__())
is being called. I'm not positive that's the call path, but that's the result. And since MyEnum.__init__ is Enum.__init__
and we're using AutoValue, the init method is expecting two arguments. It's trying to set member1.data1 = member1
and member1.data2
doesn't have a value.
要清楚,我知道正确的解决方案是将 __ str __
方法实现为 __ str__ = str .__ str __
。但是,由于 isinstance(MyEnum.member1,str)== True
和 __ str __
需要返回 str
...我不清楚为什么上面的实现会表现出来。
To be clear, I'm aware the correct solution is to implement the __str__
method as __str__ = str.__str__
. However, since isinstance(MyEnum.member1, str) == True
, and __str__
needs to return a str
... I'm unclear why the above implementation would behave how it does.
- 为什么要呼叫任何
__ init __
? - 为什么返回
self
不够,因为self
是str
? - 有些不相关,为什么不设置值就不能实现呢?如果从init字符串中删除值,请在导入<$ c时删除
__ new __
和_generate_next_value _
方法抛出$ c> TypeError 表示str()参数2必须为str,而不是int
(... / aenum / init .py:1302)。
- Why is any
__init__
getting called? - Why is it insufficient to return
self
sinceself
is anstr
? - Unrelated somewhat, by why could I not implement this without setting "value"? If you take away, value from the init string, remove the
__new__
and_generate_next_value_
methods, on import aTypeError
is thrown sayingstr() argument 2 must be str, not int
(.../aenum/init.py:1302).
推荐答案
@ user2357112有权。
@user2357112 has the right of it.
详细说明您的观点:
- 为什么不足返回
self
,因为self
是str
?
- Why is it insufficient to return
self
sinceself
is anstr
?
self
不是 str
,它是 MyEnum
,它是 str
子类-有它自己的 __ init __
,然后调用它。 str
类没有 __ init __
方法。
self
is not a str
, it's a MyEnum
which is a str
subclass -- a subclass that has it's own __init__
, which then gets called. The str
class does not have an __init__
method.
这篇关于在__str__上调用__init__会产生aenum.Enum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!