StrEnum & IntEnum: New Enum Types In Python?

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ต.ค. 2024

ความคิดเห็น • 35

  • @souplike.homogenate
    @souplike.homogenate 2 วันที่ผ่านมา +41

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

    • @phyphor
      @phyphor 2 วันที่ผ่านมา +1

      Underrated comment!

    • @Thebiggestgordon
      @Thebiggestgordon 2 วันที่ผ่านมา +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 2 วันที่ผ่านมา +8

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

  • @Madhava_P7
    @Madhava_P7 3 วันที่ผ่านมา +6

    Excited For Swift!

    • @mohammadnej7029
      @mohammadnej7029 วันที่ผ่านมา

      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 3 วันที่ผ่านมา +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.

  • @theglobalsouth9680
    @theglobalsouth9680 3 วันที่ผ่านมา +1

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

  • @МаксимАндреев-я1г
    @МаксимАндреев-я1г 3 วันที่ผ่านมา +1

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

  • @adaum79
    @adaum79 3 วันที่ผ่านมา +1

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

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 2 วันที่ผ่านมา

    Thank you 😊

  • @kellymoses8566
    @kellymoses8566 3 วันที่ผ่านมา

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

  • @ChrisHalden007
    @ChrisHalden007 3 วันที่ผ่านมา

    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 2 วันที่ผ่านมา +2

      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 วันที่ผ่านมา

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

  • @asagiai4965
    @asagiai4965 3 วันที่ผ่านมา +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 2 วันที่ผ่านมา

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

  • @MikeMMs07
    @MikeMMs07 3 วันที่ผ่านมา

    Awesome

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

  • @foxypiratecove37350
    @foxypiratecove37350 3 วันที่ผ่านมา +3

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

  • @ImKingCosmic
    @ImKingCosmic 3 วันที่ผ่านมา +1

    Awesome Sause

  • @deltamico
    @deltamico 2 วันที่ผ่านมา +2

    So you just saved 4 keystrokes when asking for a value

  • @lalropekkhawbung
    @lalropekkhawbung 13 ชั่วโมงที่ผ่านมา

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

  • @Ak4n0
    @Ak4n0 3 วันที่ผ่านมา +1

    Basically an IntEnum is a C enum.

  • @LuxFerre4242
    @LuxFerre4242 3 วันที่ผ่านมา +1

    Auto everything

  • @raidensama1511
    @raidensama1511 2 วันที่ผ่านมา

    Python: nice enums
    Rust: hold my beer.

  • @pseudotasuki
    @pseudotasuki 3 วันที่ผ่านมา +5

    These seem contradictory to the concept of enums.

  • @gurupartapkhalsa6565
    @gurupartapkhalsa6565 2 วันที่ผ่านมา +2

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

  • @isaacingleby8771
    @isaacingleby8771 3 วันที่ผ่านมา +2

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

    • @deltamico
      @deltamico 2 วันที่ผ่านมา

      what additional capabilities do they have?

    • @isaacingleby8771
      @isaacingleby8771 วันที่ผ่านมา

      @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 TH-cam comment but if you're interested I'd recommend looking up NoBoilerplate's video on enums in Rust.