I read a post recently where someone mentioned that there is no need for using enums in python. I’m interested in whether this is true or not.
For example, I use an enum to represent modem control signals:
class Signals:
CTS = "CTS"
DSR = "DSR"
...
Isn’t it better that I use if signal == Signals.CTS: than if signal == "CTS":, or am I missing something?
Signals.CTSdoes seem better than “CTS”. But Signals is not an enum, it’s a class with specific fields. The claim, as I’ve heard it, is that you don’t need a separate enum language construct, as you can do things like you’ve done in the question, or perhaps:If you have that in a signals module, it can be imported as used in a similar fashion, e.g.,
if signal == signals.CTS:. This is used in several modules in the standard library, including the re and os modules.