Another benefit of enums is that you can check for Membership instead of checking for equality, which is more performant. For example in stead of checking `if state == State.PLAYING: ...` you can write it as `if state is State.PLAYING: ...`
@@Carberraisn't that the main point to enums? To create unique constants that are part of a group. For example your MessageType enum is a group of multiple constants that have the same meaning. In a way enum members are like None (the single and unique object of type NoneType - and you cannot instantiate an other NoneType object) and should be checked for identity not just equality.
There are so many other benefits of using enums, including that they can be type checked, exhaustiveness checking can be performed to make sure all possible states are accounted for etc. If you are going to make a video on enums being "so useful", might be worth going through those, instead of, "I just like them." Otherwise it looks like you're just making videos to get clicks.
I agree: a little more about how to use them properly, different methods to access/translate values and names, then some advanced usage, like pattern matching, or gotchas. As for advantages, two (related ones) are really obvious: Code completion in your company IDE and the fact that typos in string values become proper errors.
One thing I wish Enums did is include the value name in the type representation of each Enum member. it would make TypeErrors involving Enum members way more informative.
Ive tried a few times but I keep stepping away from using enum. a follow-up vid showing explicit examples of typical scenarios without and then with enum and how enum makes it better would be very welcome.
exactly. every time i go to a new language i'm like, "alright i'm gonna learn enums and this time i'm going to find out what the actual point is" and every time i'm just like...ok that's useless lol.
@@aes0p895 they're mainly useful for consistency and readability. It can reduce silly errors when passing values to functions. You can essentially lock down what a argument is allowed to be. Lets say you have a function foo that has the argument hex_colour foo(hex_colour): ... If you don't use Enums how do you know what colour #d1580d is for example? Will you notice the difference between #d158Od and #d1580d that causes a bug? if you define an Enum you are limiting the possible choices to whatever you define from Enum import Enum class Colour(Enum): ORANGE: str = "#d1580d" BLUE: str = "#1414db" def foo(hex_colour: Colour): print(hex_colour.value) foo(Colour.ORANGE) Now you cannot accidently pass in an invalid HEX colour value to foo() You cannot accidently type the HEX colour incorrectly and end up with the wrong colour You now know that #d1580d is orange and #1414db is blue just by looking at the code. Essentially you can make values more human readable in some instances Finally, if you ever need to change the HEX value or orange you only have 1 place to change it. In the Enum definition, and not in 40,000 different places in your code base
@@aes0p895 Well if you are constantly finding yourself using strings to check something switch to an enum. It avoids spelling mistakes, they autocomplete, they can be used like a key value pair and if you refactor it will actually save a ton of typing.
@@BanAaron ah that color example is a good one. Thanks that does make sense. I think the issue I was having before is most of the examples I've seen before were values that were so easy to remember it doesn't make sense to enum them.
How to create new enum values after defining it. See my code: class Color(IntEnum): RED = 0xFF0000 GREEN = 0x00FF00 BLUE = 0x0000FF Now, I want to create new values by creating its object such as: YELLOW = (0xFFFF00) CYAN = (0x00FFFF) But it gives me error "is not a valid Color" How can I solve it?
Maybe inherit from Color like class ExtendedColor(Color): YELLOW = 0xFFFF00 But at runtime is probably not the best idea. Do this at compile time if the Color enum is inaccessible to you to my modify. Although even at runtime you can redefine a global class from everywhere but I don't recommend it
@@Carberrahow do you suppres the error raised inside the with...: block? Just return True from the __exit__ method. It took me 4 articles on Google before a learnt that. And is so simple.😅 I don't think I've seen on RUclips a complete video about context managers that would cover these bits.
This should be included in builtin. For a Java/C#/modern C++ programmers like myself, this is a wonderful feature in order to empower my newly (re)acquired Python knowledge.
I see no use for this. Maybe use a set? My opinion is that if you have a basic data structure that can do the job, it's almost always better to stick to it.
Great summary! You can also iterate over enums, which can be very helpful in certain situations!
Huh, I actually didn't know that. Thanks!
@@Carberra You’re very welcome! I watched a few more of your videos, and I really like the format. Keep it up! 🙂
Another benefit of enums is that you can check for Membership instead of checking for equality, which is more performant. For example in stead of checking `if state == State.PLAYING: ...` you can write it as `if state is State.PLAYING: ...`
Oh wow, I didn't know that! Thanks for that tip (:
Don't you mean checking for reference equality instead of object value equality? You check for membership with the "in" operator on a collection.
@@Carberraisn't that the main point to enums? To create unique constants that are part of a group. For example your MessageType enum is a group of multiple constants that have the same meaning. In a way enum members are like None (the single and unique object of type NoneType - and you cannot instantiate an other NoneType object) and should be checked for identity not just equality.
There are so many other benefits of using enums, including that they can be type checked, exhaustiveness checking can be performed to make sure all possible states are accounted for etc. If you are going to make a video on enums being "so useful", might be worth going through those, instead of, "I just like them." Otherwise it looks like you're just making videos to get clicks.
I agree: a little more about how to use them properly, different methods to access/translate values and names, then some advanced usage, like pattern matching, or gotchas.
As for advantages, two (related ones) are really obvious: Code completion in your company IDE and the fact that typos in string values become proper errors.
Thanks for explaining with the application along with the Syntax
🙂
One thing I wish Enums did is include the value name in the type representation of each Enum member.
it would make TypeErrors involving Enum members way more informative.
That would be a nice default. You could edit the __str__ (or __repr__)
Ive tried a few times but I keep stepping away from using enum. a follow-up vid showing explicit examples of typical scenarios without and then with enum and how enum makes it better would be very welcome.
exactly. every time i go to a new language i'm like, "alright i'm gonna learn enums and this time i'm going to find out what the actual point is" and every time i'm just like...ok that's useless lol.
@@aes0p895 they're mainly useful for consistency and readability. It can reduce silly errors when passing values to functions. You can essentially lock down what a argument is allowed to be.
Lets say you have a function foo that has the argument hex_colour
foo(hex_colour):
...
If you don't use Enums how do you know what colour #d1580d is for example? Will you notice the difference between #d158Od and #d1580d that causes a bug? if you define an Enum you are limiting the possible choices to whatever you define
from Enum import Enum
class Colour(Enum):
ORANGE: str = "#d1580d"
BLUE: str = "#1414db"
def foo(hex_colour: Colour):
print(hex_colour.value)
foo(Colour.ORANGE)
Now you cannot accidently pass in an invalid HEX colour value to foo()
You cannot accidently type the HEX colour incorrectly and end up with the wrong colour
You now know that #d1580d is orange and #1414db is blue just by looking at the code. Essentially you can make values more human readable in some instances
Finally, if you ever need to change the HEX value or orange you only have 1 place to change it. In the Enum definition, and not in 40,000 different places in your code base
@@aes0p895 Well if you are constantly finding yourself using strings to check something switch to an enum. It avoids spelling mistakes, they autocomplete, they can be used like a key value pair and if you refactor it will actually save a ton of typing.
@@aes0p895and that's true I never understand the enum thing
@@BanAaron ah that color example is a good one. Thanks that does make sense. I think the issue I was having before is most of the examples I've seen before were values that were so easy to remember it doesn't make sense to enum them.
what is your vscode theme?
Link in description
What shell are you using on wsl?
ZSH with OhMyZsh and Starship.
Make a video on using enums with match statement,incase of non_matcing case like _ wildcard
Enum values can't be mutable. #constant
How to create new enum values after defining it.
See my code:
class Color(IntEnum):
RED = 0xFF0000
GREEN = 0x00FF00
BLUE = 0x0000FF
Now, I want to create new values by creating its object such as:
YELLOW = (0xFFFF00)
CYAN = (0x00FFFF)
But it gives me error "is not a valid Color"
How can I solve it?
You need to define everything you might want to use within the enum itself.
Maybe inherit from Color like
class ExtendedColor(Color):
YELLOW = 0xFFFF00
But at runtime is probably not the best idea. Do this at compile time if the Color enum is inaccessible to you to my modify. Although even at runtime you can redefine a global class from everywhere but I don't recommend it
amazing as always, can u make a video about how to create a context manager I wanna make one for a db .. u know what I mean hh
That would make a good video actually! I think I've done a video on how to use them, but not how to make them, so yeah I'll deffo do that.
@@Carberrahow do you suppres the error raised inside the with...: block? Just return True from the __exit__ method. It took me 4 articles on Google before a learnt that. And is so simple.😅 I don't think I've seen on RUclips a complete video about context managers that would cover these bits.
Thanks for the nice video!
This should be included in builtin. For a Java/C#/modern C++ programmers like myself, this is a wonderful feature in order to empower my newly (re)acquired Python knowledge.
It is technically built-in -- you do have to import it, but it's part of the standard lib, so no extra installations necessary.
I didn't know asa butterfield coded too
I hate even leaving RUclips comments but you kinda never actually gave a definition of enums
Cumbersome way of defining enums, and it should be built in by default.
Guido van rossum didn't make the video lmao
Need more examples please
I see no use for this. Maybe use a set? My opinion is that if you have a basic data structure that can do the job, it's almost always better to stick to it.
Tanksyou
ENUMS OP
"why not just use the string value? Well, it's just good practice!"
That doesnt answer anything. WHY is it good practice?
There's nothing clear about instantiating an enum...