No need to list

I recently read a post in which someone mentioned that there is no need to use enums in python. I'm interested in whether this is true or not.

For example, I use an enumeration to represent modem control signals:

class Signals: CTS = "CTS" DSR = "DSR" ... 

Isn't it better if I use if signal == Signals.CTS: than if signal == "CTS": or am I missing something?

+6
source share
6 answers

Signals.CTS looks better than "CTS". But Signals is not an enumeration, it is a class with specific fields. The requirement, as I heard, is that you do not need a separate enum language construct, as you can do things like you did in this question, or perhaps:

 CTS, DSR, XXX, YYY, ZZZ = range(5) 

If you have this in a signal module, you can import it as used in a similar way, for example, if signal == signals.CTS: This is used in several modules in the standard library, including the re and os modules.

+5
source

In your exact example, I think it would be good to use certain constants, since this could cause an error when the constant is not found, alas, there will be no typo in the string.

I assume that there is at least an equal solution using object orientation.

BTW: if "CTS": will always be True , since only empty lines are interpreted as False .

+1
source

It depends on whether you use Signal.CTS , Signal.DSR values ​​as data. For example, if you send these lines to the actual modem. If so, then it would be nice to have aliases defined by you because external interfaces tend to change or be less uniform if you expect. Otherwise, if you never use character values, you can skip the abstraction layer and use strings directly.

The only thing that should not mix internal characters and external data.

0
source

If you want to have meaningful string constants ( CTS = "CTS" , etc.), you can simply do:

 for constant_name in ('CTS', 'DSR'): # All constant names go here globals()[constant_name] = constant_name 

This defines the CTS dans DSR variables with the values ​​you want. (Link on using globals() : Programmatically creating variables in Python .)

Direct definition of your constants at the top level of the module is performed in many standard library modules (for example, in the re and os modules [ re.IGNORECASE , etc.]), so this approach is pretty clean.

0
source

I think that for enumerations (setting arbitrary values, bitwise operations, descriptions of whitespace-d) much more is loaded.

Please read the very short publication below, check out the proposed enumeration class, and judge for yourself.

Python Enum Post

0
source

Do we need enums in Python? Do we need an html module, a database module, or a bool type?

I would classify Enums as pleasant, but not required.

However, part of the reason Enums finally appeared (in Python 3.4 ), because they are so nice that many people resold the listings manually. With so many private and public versions of enumerations, it becomes a problem, compatibility becomes a problem, standard use becomes a problem, etc. Etc.

So, to answer your question: No, we do not need an Enum type. But we now have one. There is even a reverse version .

0
source

Source: https://habr.com/ru/post/900023/


All Articles