4 ms·
The article is incredibly misleading. Python Enums are way more powerful than what is showcased there. First, rudimentary/basic enums are as simple as instance
by zo1 6y ago
The article is incredibly misleading. Python Enums are way more powerful than what is showcased there.
First, rudimentary/basic enums are as simple as instance variables on a class and allow super-helpful IDE "auto-completion" that doesn't necessitate the use of magic-strings to at least "set" the enum. I've been using this ridiculously simple trick since 2.5 ten years ago. Magic-strings are bad, and even worse when you have a disparate code-base in a dynamically typed language. Example:
class MyEnum(object):
Pass = 1
Fail = 2
my_value = MyEnum. <-- at this point, the IDE gives you code-completion.
Second, real Enums added as a language-feature along with type-hinting are almost 100% on-par with statically typed languages. See:
class MyEnum(Enum):
Pass = 1
Fail = 2
def my_func(did_they_pass: MyEnum):
if did_they_pass.Something: # IDE / type-checker will go nuts here.
print("Something")
Edit: Forgot the main point. Switch/case uses are only a small sub-set of the uses of Enums. So even basic/crappy enums are better than magic strings in a dynamically-typed language. Also, please don't use "list of strings" for enums in Python, there are much better ways to do that sort of thing that preclude unnecessary bugs popping up.
- throwgeorge 6y ago>real Enums added as a language-feature along with type-hinting are almost 100% on-par with statically typed languages sorry i don't understand this. i'm not trying to be pedantic but: statically typed (and type-checked) language will fail to compile ... not just warn you. that's not a 1% difference that's the entire difference (because lots of people will miss or flat out ignore ide hints).
- BiteCode_dev 6y agoSo, you don't use open("w"), do you ? You use the non magic open(os.O_WRONLY) of course. Because magic strings are bad.
- comfydragon 6y agoPython 3.8 has typing.Literal for this.
- MikeTheGreat 6y agoI will now - thanks for the tip! :) And, moreover, this will inspire me to ask "How many other traditional magic numbers/strings/etc have I gotten used to that might be defined more readably?" strcmp, I'm looking at you! I know your post was sarcastic/snarky but it was genuinely helpful
- 1wd 6y agof.tell(offset, os.SEEK_CUR) over f.tell(offset, 1) https://docs.python.org/3/library/os.html#os.SEEK_CUR https://docs.python.org/3/library/os.html#os.SEEK_CUR I wish the docs on f.tell at least mentioned the option https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects https://docs.python.org/3/tutorial/inputoutput.html#methods-...
- remram 6y agoExcept it wasn't, os.O_WRONLY is for os.open() (equivalent of open syscall, takes numeric flags), not the built-in open() (equivalent of C fopen, takes "w" and similar strings).
- 1wd 6y agoYou would have to use os.open
- BiteCode_dev 6y agoI would also have to pass the path as a first param. This was not meant to be correct, just teasy.
- zo1 6y agoThat's a fair point, and I'll be sure to keep an eye out for that type of magic-string usage going forward. Though most "magic string" usages are not nearly as official as the one you mention and usually occur in business-level code written by ordinary developers that may not be disciplined or experienced. Those spots most certainly need all the assistance they can get instead of relying on magic-strings, hence my comment of magic strings being bad.
- andrepd 6y ago>Second, real Enums added as a language-feature along with type-hinting are almost 100% on-par with statically typed languages This is conclusive proof that you're not actually aware of the capabilities of enums and sum types in "statically typed languages". Where's pattern matching? Exhaustivity checking? Sum types? GADTs?
- rumanator 6y agoC and C++ don't have some features in that list like pattern matching and there is no question that they represent one of the most popular statically typed languages in history.
- zo1 6y agoHonestly, over my nearly 20 year period of programming experience (half of which was not spent on python), I can't recall ever having encountered those terms/concepts. But on the plus side, I guess I have now and I'll be sure to read up on them. Maybe I know the concepts but haven't linked them to the terms.
- guest2143 6y agoPython 4... It's just going to be Haskell.