StrEnum & IntEnum: New Enum Types In Python?

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

КОМЕНТАРІ • 35

  • @souplike.homogenate
    @souplike.homogenate 3 дні тому +44

    StrEnum and IntEnum implies DexEnum, ConEnum, WisEnum, and ChaEnum

    • @phyphor
      @phyphor 3 дні тому +2

      Underrated comment!

    • @Thebiggestgordon
      @Thebiggestgordon 3 дні тому +1

      it also implies that we can take a +2 improvement in one of these enums when we get to python 3.12, 3.16, and 3.19!

  • @max_208
    @max_208 3 дні тому +11

    IntEnum and StringEnum are just cursed, just use .value if you want to play around with the value.

  • @theglobalsouth9680
    @theglobalsouth9680 4 дні тому +2

    super cool!!! i like the INT ENUMS A LOT

  • @МаксимАндреев-я1г
    @МаксимАндреев-я1г 3 дні тому +2

    If I need functional of str or int in enum, I make it multiple inheritance like class StrEnumn(str, Enum) or IntEnum(int, Enum)

  • @Madhava_P7
    @Madhava_P7 4 дні тому +6

    Excited For Swift!

    • @mohammadnej7029
      @mohammadnej7029 2 дні тому

      Except the fact that Swift enums are value type, allows you to access both the enum and it’s rawValue simultaneously and so much more ☺️✌️( and don’t need importing)

  • @DrDeuteron
    @DrDeuteron 4 дні тому +3

    int enums are good as bit wise flags, but after watching this video, I am going to extend normal integer (Z) operations to raise a not implemented error.

  • @adaum79
    @adaum79 4 дні тому +1

    I use StrEnum in my scripts interacting with SAP/R3 to get the component IDs and make the code cleaner

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 3 дні тому

    Thank you 😊

  • @kellymoses8566
    @kellymoses8566 4 дні тому

    I created a object to represent Windows file permissions and found the IntFlag enum very useful.

  • @ChrisHalden007
    @ChrisHalden007 4 дні тому

    Great video. Thanks

  • @davidgillies620
    @davidgillies620 3 дні тому +8

    Neat syntactic sugar, but it makes code comprehension harder. Enums aren't supposed to be integers or strings; they occupy a separate namespace. Seeing print(role) in a piece of code imposes a higher cognitive load on the reader than print(role.value). Remember: you read code much m,ore often than you write it.

    • @DelzDerMeister
      @DelzDerMeister 3 дні тому +3

      If the StrEnum is handled as string when serialising to json, I can see the benefit.
      "Role.ADMIN" or worse "" is annoying to parse for the receiver.
      And print(role) just looks like someone forgot to cleanup after debugging

    • @davidgillies620
      @davidgillies620 2 дні тому

      @@DelzDerMeister Serde between JSON and enums is language and implementation-specific. In C++ you'd probably overload >. In Rust you'd implement the Serialize and Deserialize traits. In Go you'd write custom Marshallers and Unmarshallers. It's a recognised issue with a ton of discussion on the web.

  • @deltamico
    @deltamico 3 дні тому +3

    So you just saved 4 keystrokes when asking for a value

  • @eduferreyraok
    @eduferreyraok 2 дні тому

    now I understand how does Django recognize the model field names and use it for query's and such... if you apply auto over the model attrs, BUT i still don't know how does recognize the model class name.

  • @MikeMMs07
    @MikeMMs07 4 дні тому

    Awesome

  • @raidensama1511
    @raidensama1511 3 дні тому

    Python: nice enums
    Rust: hold my beer.

  • @ImKingCosmic
    @ImKingCosmic 4 дні тому +1

    Awesome Sause

  • @asagiai4965
    @asagiai4965 4 дні тому +4

    I think the only thing I like here is the auto function.

    • @davidgillies620
      @davidgillies620 3 дні тому +2

      The problem is that _enums are not integers or strings_ . It's a mistake to think of enum members as "being" 1, 2, 3 or 'foo', 'bar', 'baz' etc.. They should be opaque and only compared with each other. An enum is supposed to encapsulate a set of related constants and be a first class object in its own right. You should never need to know the underlying value of an enum. If you're mapping HTTP status codes to something, for example, use a dictionary to map the code to an enum value and use that value subsequently.

    • @asagiai4965
      @asagiai4965 3 дні тому +1

      @@davidgillies620 I didn't say enums are integer or string.
      But ok I guess.

  • @stroudcuster4483
    @stroudcuster4483 3 дні тому

    StrEnum would be useful when you want to use the enum value as an SQL column value

  • @foxypiratecove37350
    @foxypiratecove37350 4 дні тому +3

    StrEnum is cursed, from a C/C++ & Asm & Machine code developer.

  • @lalropekkhawbung
    @lalropekkhawbung День тому

    'Cross-eyed' , ' tongue out ', I wonder why you mentioned that specifically 🤔

  • @Ak4n0
    @Ak4n0 4 дні тому +1

    Basically an IntEnum is a C enum.

  • @LuxFerre4242
    @LuxFerre4242 3 дні тому +1

    Auto everything

  • @pseudotasuki
    @pseudotasuki 4 дні тому +6

    These seem contradictory to the concept of enums.

  • @gurupartapkhalsa6565
    @gurupartapkhalsa6565 3 дні тому +2

    Once you bother implementing StrEnum, it might as well be InstanceEnum. What a short-sighted waste of implementation details

  • @isaacingleby8771
    @isaacingleby8771 4 дні тому +2

    After using enums and the match statement in Rust, those features are so disappointing in other languages

    • @deltamico
      @deltamico 3 дні тому

      what additional capabilities do they have?

    • @isaacingleby8771
      @isaacingleby8771 2 дні тому

      @deltamico a lot of it is helped by Rust being a statically typed language, but the match statement assigning values like `let x = match enum_value {...}`. On top of that the match statement can check that you've exhausted all options, so with enums where only a limited set of states are possible, the Rust compiler is clever enough to realise you've left out a possibility and stop compilation from progressing.
      You can also store distinct objects within different enum values, which is really handy, which means you don't need string enums and int enums, you can just assign branch one a string and branch two an integer if you so wish.
      It's hard to demo this in a UA-cam comment but if you're interested I'd recommend looking up NoBoilerplate's video on enums in Rust.