> On 31 May 2021, at 18:24, Peter Otten <__pete...@web.de> wrote:
>
> On 31/05/2021 17:57, Colin McPhail via Python-list wrote:
>> Hi,
>> According to the enum module's documentation an Enum-based enumeration's
>> members can have values of any type:
>> "Member values can be anything: int, str, etc.."
>
> You didn't read the fineprint ;)
>
Ah, neither I did.
> """
> The rules for what is allowed are as follows: names that start and end with a
> single underscore are reserved by enum and cannot be used; all other
> attributes defined within an enumeration will become members of this
> enumeration, with the exception of special methods (__str__(), __add__(),
> etc.), descriptors (methods are also descriptors), and variable names listed
> in _ignore_
> """"
>
> Functions written in Python are descriptors and therefore cannot be used.
> Builtin functions aren't, leading to the following somewhat surprising
> difference:
>
> >>> def foo(self): print("foo")
>
> >>> class A(Enum):
> x = foo # foo.__get__ exists -> foo is a descriptor
> # the enum library assumes you are adding a
> # method to your
> # class, not an enumeration value.
>
> y = sum # no sum.__get__ attribute -> function
> # treated as an enumeration value.
>
> >>> list(A)
> [<A.y: <built-in function sum>>]
> >>> A.x
> <function foo at 0x012096A0>
> >>> A.y
> <A.y: <built-in function sum>>
> >>> A.y.x()
> foo
>
Thank you for explaining it.
Regards,
Colin
--
https://mail.python.org/mailman/listinfo/python-list