Are metaclasses the HARDEST thing in Python?

preview_player
Показать описание



Metaclasses are considered one of the hardest, if not THE hardest, thing in Python's core functionality. But is it really as bad as people claim? In this video we'll cover the basics of metaclasses, look at a real-world example, and discuss when they should be used.

00:00 - Intro [1]
02:11 - Dynamically creating classes using the type built-in
05:51 - Creating custom metaclasses [2]
11:14 - Metaclasses aren't always the answer [3]
14:26 - Creating classproperties using metaclasses [4-7]
19:47 - Outro



If you want to see more of me, join my Discord server!

I get a lot of people asking, so here's my setup!

Affiliations (I may earn a commission):



If you have any questions, don't hesitate to ask in the comments! I'll try and answer as soon as I can, providing someone else hasn't already done so.

#python #coding #howto
Рекомендации по теме
Комментарии
Автор

You've done a really good job at explaining what metaclasses are. I am currently researching ways to do this in Cython or Rust at a low level to try and optimize modern dataclass libraries. I'll be sure to save this video somewhere as a reference.

vizonexbuilds
Автор

Metaclasses are probably less spoken topic in python, but by far not one of the most complicated subjects

blanky_nap
Автор

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.

yaroslavdon
Автор

Actually the basic concept is quite easy: everything in Python is an object, including classes. And every object is an instance of a class. Since a class is an objects, it is itself an instance of a class, which is called metaclass. Normally that class is the class type, but by the Liskov subtitution principle, you should be able to use any subclass of type instead.

__christopher__
Автор

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

Dhtkna
Автор

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 +

VivekKumarSinha
Автор

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

k
Автор

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)

marckiezeender
Автор

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?

maleldil
Автор

Why do your timestamps have wrong ordering?
It starts with "Intro [1]", which is wrong... it should have been "Intro [0]"

lostsmile
Автор

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?

richardbennett
Автор

too complicated that I didn't understand😮

OmidAtaollahi
Автор

Spelling.

Spelling is the "trickest" thing.

capnmnemo
visit shbcf.ru