Like c enumeration in python3 [ERRATA]
Terry Reedy
tjreedy at udel.edu
Mon Mar 23 01:13:46 EDT 2020
On 3/22/2020 11:37 PM, DL Neil via Python-list wrote:
> On 23/03/20 3:32 PM, Paulo da Silva wrote:
>> Às 02:18 de 23/03/20, Paulo da Silva escreveu:
>>> Hi!
>
> Olá,
>
>
>>> Suppose a class C.
>>> I want something like this:
>>>
>>> class C:
>>> KA=0
>>> KB=1
>> KC=2
>>> ...
>>> Kn=n
>>>
>>> def __init__ ...
>>> ...
>>>
>>>
>>> These constants come from an enum in a .h (header of C file).
>>> They are many and may change from time to time.
>>> Is there a way to somehow define them from inside __init__ giving for
>>> example a list of names as strings?
>>> There is an additional problem: C is not recognized inside __init__!
>
>
> Please read the PSL docs carefully, because Python's enums seem to
> differ from those in other languages - sometimes in significant ways,
> and sometimes in a subtle manner!
>
>
> I have been asking similar questions recently - particularly wanting to
> (also) use the "strings", because of the effort of upgrading many
> modules of code at the same time (the manual, or was it the PEP(?)
> refers to the need to upgrade the PSL to use enums, and to the effort
> that might cost - I suggest there has been little/v.slow take-up, but
> then keeping the PSL updated is a subject of a great many conversations,
> elsewhere).
>
> Python's enums cannot be changed. Once set, C.KA cannot be changed to 1
I am not sure what you are saying. The OP's class is not an enum in the
enum module sense. Class attributues are usually mutable.
>>> class C:
K = 0
>>> C.K = 1
>>> C.K
1
--
Terry Jan Reedy
More information about the Python-list
mailing list