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 +
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.
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...
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
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)
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?
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.
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.
I've used metaclasses professionally several times over the years. This is the best deep dive on the topic I am aware of: ua-cam.com/video/sPiWg5jSoZI/v-deo.html
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?
Head to ultahost.com to host your website or application for less!
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 +
Yeah I noticed it in editing and thought I should correct myself lmao.
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.
I'll have a look at those sources, thanks for the suggestion!
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...
Don't even look for a deeper dive, classes are rubbish. Do OOP paradigm and save yourself unnecessary abstractions.
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
Metaclasses are probably less spoken topic in python, but by far not one of the most complicated subjects
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)
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?
Setting the metaclass to ABCMeta is _slightly_ faster than inheriting from ABC. That's the only difference.
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.
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.
Why do your timestamps have wrong ordering?
It starts with "Intro [1]", which is wrong... it should have been "Intro [0]"
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.
I've used metaclasses professionally several times over the years. This is the best deep dive on the topic I am aware of: ua-cam.com/video/sPiWg5jSoZI/v-deo.html
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?
too complicated that I didn't understand😮
Spelling.
Spelling is the "trickest" thing.
Good job the thumbnail is AB tested so only half the viewers saw that lmao.
Trickiest ***
This has been fixed, thanks for bringing it to my attention!
Someone is wrong on the Internet is the strongest engagement factor.
Literally yeah. Though I don't like to be wrong on purpose!