Are metaclasses the HARDEST thing in Python?

  Рет қаралды 5,301

Carberra

Carberra

Күн бұрын

Пікірлер: 25
@Carberra
@Carberra 2 ай бұрын
Head to ultahost.com to host your website or application for less!
@VivekKumarSinha25
@VivekKumarSinha25 2 ай бұрын
5:40 the class you created using "class" keyword is not the same as one you created using type, although it may appear to be, with only difference being, name, age and job are class level attributes when created using "type" built-in, meaning all instance of Profile will share those attributes with same values. While in 2nd case, each object needs to initialized with those parameters, since no defaults have been specified. It's one difference, but a big one. EDIT: thanks for clarifying it later 8:00 +
@Carberra
@Carberra 2 ай бұрын
Yeah I noticed it in editing and thought I should correct myself lmao.
@yaroslavdon
@yaroslavdon 2 ай бұрын
In my experience, meta-classes are used to change class behaviour and to better encapsulate said behaviour. The two prominent examples are ABCMeta and EnumMeta (from abc and enum packages, respectively). ABCMeta makes a class, well, abstract. It also adds useful features (like recognizing @abstractmethod) and handles virtual superclass registry. EnumMeta is much more complex, as it needs to configure the full behaviour of an Enum (which is a very special class). For example, it needs to properly describe inheritance action, the way to pickle items, etc. I suggest reading these two metaclasses' code; it teaches a lot. I've had another-rather unusual-usage of metaclasses is for type annotations. I've used helper functions that accept a general enum class as an attribute (to iterate over the enum). The enum's type is EnumMeta.
@Carberra
@Carberra 2 ай бұрын
I'll have a look at those sources, thanks for the suggestion!
@k283
@k283 2 ай бұрын
It's wrong at 7:00: a class object is created during ~~new~~ of the metaclass, not during ~~init~~. By the time metaclass' ~~init~~ runs, a class object already exists. So in metaclasses, ~~init~~ is more like a class decorator, which is applied to subclasses as well (because they share the same metaclass) Sadly, this video has only some shallow basics and syntax, not really a deep dive. You didn't explain how metaclasses work - only some syntax on how to use them...
@AhmedIsam
@AhmedIsam 2 ай бұрын
Don't even look for a deeper dive, classes are rubbish. Do OOP paradigm and save yourself unnecessary abstractions.
@Dht1kna
@Dht1kna 2 ай бұрын
Idk perhaps CAPI extensions to overcome GIL bottlenecks sound much harder, reflection stuff with code-gen might be harder too. Customizing Garbage collection stuff, heck even effectively using weakrefs by itself, are harder too imo
@blanky_nap
@blanky_nap 2 ай бұрын
Metaclasses are probably less spoken topic in python, but by far not one of the most complicated subjects
@marckiezeender
@marckiezeender 2 ай бұрын
13:50 There is a problem with your singleton base class: the dunder init method in the subclass gets run every time you call DatabaseManager, in this case twice, which probably isn't intended behavior, especially if that method connects twice to a database. I guess a metaclass is better after all! Also, it's possible to make an actual classproperty using a descriptor (although it won't be able to override assignment)
@maleldil1
@maleldil1 2 ай бұрын
I don't quite understand when it's appropriate to use them. Also, I wish you'd covered ABCMeta vs ABC. I usually go for ABC, but when should ABCMeta be used instead?
@Carberra
@Carberra 2 ай бұрын
Setting the metaclass to ABCMeta is _slightly_ faster than inheriting from ABC. That's the only difference.
@legion_prex3650
@legion_prex3650 2 ай бұрын
Image you have to build some kind of framework for your coworkers. They have to use some of your classes. And you wanna make sure, they use the classes the proper way. For example, they have to define a string, which is a path to a Json-File. You can now check in the metaClass, that they indeed used the proper file-suffix in that string. Stuff like that. You will probably not need it for your own code. Python itself is packed with Metaclasses.
@jlp2011
@jlp2011 2 ай бұрын
They are, more than a bit, deep black magic. However, if you want to configure attributes at the class, not instance, level, they can be very powerful, esp when there are relationships between class attributes. Let’s say you have an RPG with warrior classes. A TwohandedSwordFighter class could be a specialized class that takes 1 and only 1 sword in its init (using kwargs and super calls). While a ShieldFighter would take one weapon and 1 shield. You could configure that behavior with mixins and a class-level weapons attribute that is “merged” by metaclassing. Is it the best way to model this? Not necessarily but it is one possible approach.
@lostsmile6792
@lostsmile6792 2 ай бұрын
Why do your timestamps have wrong ordering? It starts with "Intro [1]", which is wrong... it should have been "Intro [0]"
@Carberra
@Carberra 2 ай бұрын
Not sure if this is a joke about zero-indexing or not haha. In case it isn't, the [1] matches up to the first reference, listed below the chapters.
@stharding1
@stharding1 2 ай бұрын
I've used metaclasses professionally several times over the years. This is the best deep dive on the topic I am aware of: kzbin.info/www/bejne/qYHMiJprn7iikKs
@richardbennett4365
@richardbennett4365 2 ай бұрын
Definitely 😮😂 must be the hardest thing to teach, too. Did anyone understand the presentation? He forgot to explain the structure and element and instead just wrote an example of how these structures and elements are invoked. His audience must be for those who already understand these metaclasses. Then, in the middle the presenter said he did something differently from how he meant to do something? Why wouldn't he redo his presentation so the wrong part is not still in there? Why?
@OmidAtaollahi
@OmidAtaollahi Ай бұрын
too complicated that I didn't understand😮
@capnmnemo
@capnmnemo 2 ай бұрын
Spelling. Spelling is the "trickest" thing.
@Carberra
@Carberra 2 ай бұрын
Good job the thumbnail is AB tested so only half the viewers saw that lmao.
@xj0ex39
@xj0ex39 2 ай бұрын
Trickiest ***
@Carberra
@Carberra 2 ай бұрын
This has been fixed, thanks for bringing it to my attention!
@capnmnemo
@capnmnemo 2 ай бұрын
Someone is wrong on the Internet is the strongest engagement factor.
@Carberra
@Carberra 2 ай бұрын
Literally yeah. Though I don't like to be wrong on purpose!
There's 151 itertools functions you didn't know about
12:27
Carberra
Рет қаралды 3,6 М.
Domain-Driven Design: The Last Explanation You'll Ever Need
21:05
Software Developer Diaries
Рет қаралды 14 М.
So Cute 🥰 who is better?
00:15
dednahype
Рет қаралды 19 МЛН
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 23 МЛН
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
Writing My Own Database From Scratch
42:00
Tony Saro
Рет қаралды 261 М.
Generics are VITAL in typed Python
16:54
Carberra
Рет қаралды 11 М.
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 65 М.
Metaclasses in Python
15:45
mCoding
Рет қаралды 157 М.
Pydantic is OP, here's why
18:10
Carberra
Рет қаралды 24 М.
15 POWERFUL Python Libraries You Should Be Using
22:31
ArjanCodes
Рет қаралды 64 М.
5 Useful Python Decorators (ft. Carberra)
14:34
Indently
Рет қаралды 109 М.
SQLModel + FastAPI: Say Goodbye to Repetitive Database Code
19:50
Python laid waste to my C++!
17:18
Sheafification of G
Рет қаралды 135 М.
Metaclasses in 17 minutes
17:29
Carberra
Рет қаралды 5 М.