Enums in Python are SO useful

Поделиться
HTML-код
  • Опубликовано: 6 янв 2025

Комментарии • 44

  • @saxumnatur
    @saxumnatur Год назад +9

    Great summary! You can also iterate over enums, which can be very helpful in certain situations!

    • @Carberra
      @Carberra  Год назад +1

      Huh, I actually didn't know that. Thanks!

    • @saxumnatur
      @saxumnatur Год назад

      @@Carberra You’re very welcome! I watched a few more of your videos, and I really like the format. Keep it up! 🙂

  • @tyrelllaszlo6024
    @tyrelllaszlo6024 2 года назад +9

    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: ...`

    • @Carberra
      @Carberra  2 года назад +3

      Oh wow, I didn't know that! Thanks for that tip (:

    • @AndyTutify
      @AndyTutify Год назад +3

      Don't you mean checking for reference equality instead of object value equality? You check for membership with the "in" operator on a collection.

    • @Victor_Marius
      @Victor_Marius Год назад

      ​@@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.

  • @jackgenewtf
    @jackgenewtf 2 года назад +36

    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.

    • @malteplath
      @malteplath 2 года назад +2

      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.

  • @keerthi5051
    @keerthi5051 5 месяцев назад

    Thanks for explaining with the application along with the Syntax
    🙂

  • @teslainvestah5003
    @teslainvestah5003 2 года назад +3

    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.

    • @lattice737
      @lattice737 Год назад

      That would be a nice default. You could edit the __str__ (or __repr__)

  • @jackrorystaunton4557
    @jackrorystaunton4557 Год назад +5

    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.

    • @aes0p895
      @aes0p895 Год назад +2

      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.

    • @BanAaron
      @BanAaron Год назад +6

      @@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

    • @charg1nmalaz0r51
      @charg1nmalaz0r51 Год назад

      @@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.

    • @demythekidd888
      @demythekidd888 Год назад

      ​@@aes0p895and that's true I never understand the enum thing

    • @aes0p895
      @aes0p895 Год назад +1

      @@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.

  • @LuizHenrique-qr3lt
    @LuizHenrique-qr3lt 9 месяцев назад

    what is your vscode theme?

    • @Carberra
      @Carberra  8 месяцев назад

      Link in description

  • @doowi1182
    @doowi1182 2 года назад +1

    What shell are you using on wsl?

    • @Carberra
      @Carberra  2 года назад

      ZSH with OhMyZsh and Starship.

  • @SP-db6sh
    @SP-db6sh Год назад +3

    Make a video on using enums with match statement,incase of non_matcing case like _ wildcard

  • @sketchwaretagalogtutorials
    @sketchwaretagalogtutorials 2 года назад +4

    Enum values can't be mutable. #constant

  • @Policy_Maker
    @Policy_Maker Год назад

    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?

    • @Carberra
      @Carberra  Год назад +2

      You need to define everything you might want to use within the enum itself.

    • @Victor_Marius
      @Victor_Marius Год назад +1

      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

  • @0xtz_
    @0xtz_ 2 года назад +2

    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

    • @Carberra
      @Carberra  2 года назад +2

      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.

    • @Victor_Marius
      @Victor_Marius Год назад

      ​@@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.

  • @wcnv
    @wcnv 8 месяцев назад

    Thanks for the nice video!

  • @MohammadNajmiBachok
    @MohammadNajmiBachok 2 года назад +2

    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.

    • @Carberra
      @Carberra  2 года назад +3

      It is technically built-in -- you do have to import it, but it's part of the standard lib, so no extra installations necessary.

  • @TheVideoVolcano
    @TheVideoVolcano Год назад

    I didn't know asa butterfield coded too

  • @musicmarketing
    @musicmarketing 6 месяцев назад

    I hate even leaving RUclips comments but you kinda never actually gave a definition of enums

  • @havenisse2009
    @havenisse2009 2 года назад +1

    Cumbersome way of defining enums, and it should be built in by default.

    • @Red_Schuhart
      @Red_Schuhart Год назад

      Guido van rossum didn't make the video lmao

  • @chriskeo392
    @chriskeo392 Год назад

    Need more examples please

  • @V1kToo
    @V1kToo Год назад

    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.

  • @aynul1234
    @aynul1234 9 месяцев назад

    Tanksyou

  • @someabsolutelyrandomstuff9440
    @someabsolutelyrandomstuff9440 2 года назад

    ENUMS OP

  • @danielschmider5069
    @danielschmider5069 10 месяцев назад

    "why not just use the string value? Well, it's just good practice!"
    That doesnt answer anything. WHY is it good practice?

  • @pointer333
    @pointer333 10 месяцев назад +2

    There's nothing clear about instantiating an enum...