Different Ways To Define Enums // Python Tips

preview_player
Показать описание


🎓 Courses:

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Sybren A. Stüvel
- Dale Hagglund

🔖 Chapters:
0:00 Intro

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

This could lead to issues if you use the auto for choices that you are gonna store in a DB, and later on the code changes and different integers are now mapped to different "constants"

AgustinMarquezBraconi
Автор

Very nice, I really like these kind of videos. Thanks!

megalodon
Автор

I like this kind of video, there are quite engaging and give info in direct way

treblaoj
Автор

That's a solution for my headache. Often, I need to re arrange my Enums. At that time, it's quite difficult to delete the current value and add new values.

kcvinu
Автор

I also find inheriting from both (str, Enum) quite useful

stradivari
Автор

It's important to notice that the result is not the same in both cases as auto() sets the first element to 1, range() will set it to 0.

JorgeLuis-tsqp
Автор

How about with powers of 2 so that the CEO could also be President?

budoray
Автор

Cool but the first way is more readable.

nowyouknow
Автор

I like to capitalize the enum class name as well.

lawrencedoliveiro
Автор

It seems a bit less readable compared to the original implementation. However, nice tips 😊

rungxanh
Автор

You need to be careful here, if you ever serialize the enum object (safe to a database or file for instance). It will likely use the number, not the name (depending on how you serialize). When inserting new options without explicitly asigning numbers, the serialized data might be interpreted differently, after you load them back into your program when you autogenerate the numbers.

typinowly
Автор

the code is so tiny it's almost unreadable. But i can see the narrator so well!

johnaweiss
Автор

i don't understand what the point of an enum is if you just have to assign another variable to it anyway, then use a special function to extract the value. variables give you the value immediately. i know i'm missing something but i swear enums have always seemed useless lol.

aesp
Автор

Enum should be fixed. If you use dynamic numbering you may run into issues when client code relay on a value.

gadeichhorn
Автор

is it wrong if I give str values instead of numbers to my enums?

Empristis
Автор

Mmm… It should be super explicit.
Why use auto or a range when you have four fields?
And if you have more, having an automatic number assignment just complicates the code IMHO.

MagnusAnand
Автор

Hmm, so Albo wants to replace FF generation with 'renewables' aka unreliable.
Has he figured out how many wind turbines would be required to do that? How many wind turbines would you need to supply the required electricity when the wind isn't blowing?

ChrisBNisbet
Автор

Why use a class for an enum? Why not a dict? Serious question btw.

Efecretion
Автор

Assigning enums as 0 is an antipattern
If you don't want Role.CEO to be Falsy.

jacobshore