Metaclasses in 17 minutes

preview_player
Показать описание
One of the most confusing aspects of Python, explained.



If you enjoy my content, consider supporting me on Patreon or becoming a member!

If you need help with anything, feel free to join the Discord server:

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



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
Рекомендации по теме
Комментарии
Автор

Basically metaclasses make the most sense when (a) you want to tinker with the fundamental lifecycle of objects without altering the mechanisms of the programming language itself, and (b) it would be inappropriate to have an implication of any subtyping relationship caused only by the desire to do (a) - and in fact there may already be an existing subtype hierarchy that you can't replace anyways. So overriding __new__ is a good case for that, and additional examples of it would be if you want to institute a singleton or multiton pattern for instance creation without forcing everybody to deal with factory-method boilerplate calls everywhere. That's what the Path() vs PosixPath() example effectively was; while I doubt pathlib does this you could conceptually use a metaclass there to implement a multiton over the space of strings supplied as paths (which no doubt sounds weird... until you start to think about adding in functionality for Python-aware locking of directories or files). I don't think there is really any conflict between what inheritance is good for vs when to use metaclasses; really it just boils down to "gnarly details that have nothing to do with type relationships but do make sense to wire into multiple unrelated classes without breaking existing logic that was never metaclass logic in the first place". If you want more reading material on the topic, refer to Kizales "The Art of the Metaobject Protocol". His work is the earliest significant material I recall being published on the topic, and should be a good place to hunt for papers or books that have him as a reference source.

reidpinchback
Автор

4:10 - That's not quite correct. What that actually does is creating a class attribute, not an instance attribute. See here:
>>> Foo = type('Foo', (), {'x': 123})
>>> Foo.x
123

pillmuncher
Автор

use case: you got a buncha classes that all work just a little bit different from each other, but share enough in common, to save yourself about 3 miles of boilerplate code.

ronboprime
Автор

So you're saying I can connect to a database in a metaclass and whenever I define a class which needs to interact with the database it just uses this metaclass. Sounds nice! But probably not something durable for the long term

jeroenvermunt
Автор

Is there a reason why meta classes are so much used in the django framework?

flx
Автор

Using type to create classes would be useful if you wanted to generate your classes dynamically. Polymorphic mutating code, anyone? :)

terrysimons