[Python-ideas] Change magic strings to enums

Jacco van Dorp j.van.dorp at deonet.nl
Wed Apr 25 04:06:56 EDT 2018


> First, though, can you enumerate (pun intended) the problems with
> magic strings? You list "no magic strings" as a benefit, as if it's
> self-evident; I'm not sure that it is.
>
> ChrisA

One of my main reasons would be the type-checking from tools like
Pycharm, which is the one I use. If I don't remember the exact
strings, I won't have to switch to my browser to look up the
documentation, but instead I type the enum name, and the typechecker
will give me the members with correct spelling - all I need to
remember is a vague idea of what option did what. The option names
will be reminders instead of the thing to remember.

Perhaps the string encode/decode would be a better case, tho. Is it
latin 1 or latin-1 ? utf-8 or UTF-8 ? They might be fast to look up if
you know where to look (probably the top result of googling "python
string encoding utf 8", and it's the second and first option
respectively IIRC. But I shouldn't -have- to recall correctly), but
it's still a lot faster if you can type "Encoding.U" and it gives you
the option.

I'll go and see if I can make a small list of modules using these kind
of strings that aren't of the essential core when I get home this
evening.

My apologies if magic strings isn't the correct word. Despite that, I
believe everyone knows what I intend to say.


More information about the Python-ideas mailing list