StrEnum & IntEnum: New Enum Types In Python?

  Рет қаралды 14,804

Indently

Indently

Күн бұрын

Пікірлер: 43
@souplike.homogenate
@souplike.homogenate Ай бұрын
StrEnum and IntEnum implies DexEnum, ConEnum, WisEnum, and ChaEnum
@phyphor
@phyphor Ай бұрын
Underrated comment!
@Thebiggestgordon
@Thebiggestgordon Ай бұрын
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!
@Tom_Seeker
@Tom_Seeker Ай бұрын
Please cover flags next , which are part of enum package . Perhaps you could also enlighten on the meaning of flag boundaries (strict, keep etc.) 😊
@adaum79
@adaum79 Ай бұрын
I use StrEnum in my scripts interacting with SAP/R3 to get the component IDs and make the code cleaner
@theglobalsouth9680
@theglobalsouth9680 Ай бұрын
super cool!!! i like the INT ENUMS A LOT
@DrDeuteron
@DrDeuteron Ай бұрын
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.
@TaleshicMatera
@TaleshicMatera 19 күн бұрын
The enum module has a dedicated Flag type for bit-wise flags/masks where :D
@МаксимАндреев-я1г
@МаксимАндреев-я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)
@tenebrae711
@tenebrae711 11 күн бұрын
I thought it's not possible to inherit from builtins like str, int, slice etc...
@dipeshsamrawat7957
@dipeshsamrawat7957 Ай бұрын
Thank you 😊
@ChrisHalden007
@ChrisHalden007 Ай бұрын
Great video. Thanks
@PJutch
@PJutch 26 күн бұрын
5:20 I assume that doing it the other way around, like `input() == Role.ADMIN` is more useful
@kellymoses8566
@kellymoses8566 Ай бұрын
I created a object to represent Windows file permissions and found the IntFlag enum very useful.
@Madhava_P7
@Madhava_P7 Ай бұрын
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)
@deltamico
@deltamico Ай бұрын
So you just saved 4 keystrokes when asking for a value
@MikeMMs07
@MikeMMs07 Ай бұрын
Awesome
@asagiai4965
@asagiai4965 Ай бұрын
I think the only thing I like here is the auto function.
@davidgillies620
@davidgillies620 Ай бұрын
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 Ай бұрын
@@davidgillies620 I didn't say enums are integer or string. But ok I guess.
@eduferreyraok
@eduferreyraok Ай бұрын
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.
@stroudcuster4483
@stroudcuster4483 Ай бұрын
StrEnum would be useful when you want to use the enum value as an SQL column value
@balderus6562
@balderus6562 Ай бұрын
But if Comparisons are bad, what is the advantage of these? Is it just useful with auto()?
@ImKingCosmic
@ImKingCosmic Ай бұрын
Awesome Sause
@lalropekkhawbung
@lalropekkhawbung Ай бұрын
'Cross-eyed' , ' tongue out ', I wonder why you mentioned that specifically 🤔
@Ak4n0
@Ak4n0 Ай бұрын
Basically an IntEnum is a C enum.
@foxypiratecove37350
@foxypiratecove37350 Ай бұрын
StrEnum is cursed, from a C/C++ & Asm & Machine code developer.
@LuxFerre4242
@LuxFerre4242 Ай бұрын
Auto everything
@raidensama1511
@raidensama1511 Ай бұрын
Python: nice enums Rust: hold my beer.
@davidgillies620
@davidgillies620 Ай бұрын
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 Ай бұрын
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.
@TreyKeown
@TreyKeown Ай бұрын
I agree to an extent, but I think it's fine. Especially coming from the world of C enums, the idea of an enumeration being a value itself is intuitive.
@max_208
@max_208 Ай бұрын
IntEnum and StringEnum are just cursed, just use .value if you want to play around with the value.
@pseudotasuki
@pseudotasuki Ай бұрын
These seem contradictory to the concept of enums.
@gurupartapkhalsa6565
@gurupartapkhalsa6565 Ай бұрын
Once you bother implementing StrEnum, it might as well be InstanceEnum. What a short-sighted waste of implementation details
@isaacingleby8771
@isaacingleby8771 Ай бұрын
After using enums and the match statement in Rust, those features are so disappointing in other languages
@deltamico
@deltamico Ай бұрын
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 KZbin comment but if you're interested I'd recommend looking up NoBoilerplate's video on enums in Rust.
@stdprocedure
@stdprocedure Ай бұрын
@@deltamico you can do things like struct Cat; struct Dog; enum Animal { Cat(Cat), Dog(Dog), Idk { something: String, }, Other(String), } let animal = Animal::Other("parrot"); match animal { Cat(my_cat) => println!("cat: {my_cat}"), Idk { something } => println!("{something}"), _ => panic!("Crash the program, this should never happen"); } it's mandatory to exhaust every situation, so, either you should use "_" as a "fallback" branch, or you should include all variants (Dog, Other)
What does '__init__.py' do in Python?
6:50
Indently
Рет қаралды 86 М.
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 451 М.
10 Nooby Mistakes Devs Often Make In Python
24:31
Indently
Рет қаралды 67 М.
Python's 5 Worst Features
19:44
Indently
Рет қаралды 111 М.
Python dataclasses will save you HOURS, also featuring attrs
8:50
20 Everyday Tips & Tricks in Python
25:18
Indently
Рет қаралды 26 М.
5 Python Libraries You Should Know in 2025!
22:30
Keith Galli
Рет қаралды 20 М.
All 39 Python Keywords Explained
34:08
Indently
Рет қаралды 208 М.
Avoid These BAD Practices in Python OOP
24:42
ArjanCodes
Рет қаралды 73 М.
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 63 М.