Python's enum - Start Building Enumerations

Поділитися
Вставка
  • Опубліковано 19 жов 2024

КОМЕНТАРІ • 7

  • @saeedfarmanali
    @saeedfarmanali 5 місяців тому

    thanks .man

  • @adityahpatel
    @adityahpatel 5 місяців тому +1

    but what is the use case? you haven't explained

    • @timoleksii
      @timoleksii 3 місяці тому

      It was explained -- single point of truth, readability and ease of debugging.

    • @mkaberli614
      @mkaberli614 2 місяці тому

      @@timoleksii Yes, but, other than readability, I'd like to know why it's useful. I'm sure it has value, but I've not seen a anyone actually demonstrate that.

    • @timoleksii
      @timoleksii 2 місяці тому

      ​@@mkaberli614 I have quite a bit years of C++ and this language prefers using enums, however, they're not that rich of features. The reasons to use them:
      1. If you use enums, you bring more clarity to the code.
      Recently I create a script that parses JSON. There is a piece of code that analysis whether JSON has got logically correct structure. It returns enum `OK` if everything is OK, `NOT_ENOUGH_ARGS` if some arguments are missing, or other values. This looks much better than just returing `1` or `2`., or other values.
      2. It's easier to debug.
      For instance, your function returns a string that can have multiple variants. Let's imagine that you expect the function to return `OK`, `Not enough arguments`. Your collegue modifies the code and adds one more clause that forces function to return `Ok` (instead of `OK`). How long will it take ro figure this out? Everyone's experience is different but this kind of pesky mistake forced my to do overtimes and leave my office very late.
      You can get used to finding mistakes like this quite quickly, but isnt it better to just return some enum value which interpreter checks for you? If you return EnumValue.Ok instead of EnumValue.OK, the interpreter will find it and report it to you.
      3. Single point of truth.
      Even if you're not convinced with #2, let's imagine that you need to change all string `Not enough arguments` in your code to something else. How long will it take? Are you sure that this is not `not enough arguments`? If you use conversion of enum --> string, you can avoid such kind of mistakes in advance. You create a function that converts enum to some string and use it in place you need. it looks much better if you need to refactor it afterwards, as enum --> string incapsulated in only one place.

    • @timoleksii
      @timoleksii 2 місяці тому

      @@mkaberli614 I wrote a big explanation about it, however, it seems to got lost.
      Anyway, it all boils down to following things:
      1. you transfer responsibility for checking values / strings into interpreter (python check enums better than strings, compare "OK" string and Result.OK enum value)
      2. you have better code control (if you need to change enum, it's more controlable to do it)

    • @timoleksii
      @timoleksii 2 місяці тому

      @@mkaberli614 lol the message was deleted once again