Metaclasses in 17 minutes

  Рет қаралды 4,150

Carberra

Carberra

Жыл бұрын

One of the most confusing aspects of Python, explained.
-
If you enjoy my content, consider supporting me on Patreon or becoming a member!
patreon.carberra.xyz
join.carberra.xyz
If you need help with anything, feel free to join the Discord server:
discord.carberra.xyz
I get a lot of people asking, so here's my Visual Studio Code setup!
• My Visual Studio Code ...
-
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

Пікірлер: 10
@reidpinchback8850
@reidpinchback8850 10 ай бұрын
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.
@pillmuncher67
@pillmuncher67 Жыл бұрын
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
@Carberra
@Carberra Жыл бұрын
Good spot.
@BboyKeny
@BboyKeny Жыл бұрын
So like a static attribute/property?
@pillmuncher67
@pillmuncher67 Жыл бұрын
@@BboyKeny There's really nothing static about Python. It's all pretty dynamic. That said, yes, other languages call that a static attribute/property/member. In Python pretty much everything is an object and can be referenced by a variable or attribute. Methods are just functions and are referenced through a name in the class dict: >>> class Foo: ... ... >>> def baz(self, x): ... return x * 2 ... >>> Foo.bar = baz >>> f = Foo() >>> f.bar(123) 246
@reidpinchback8850
@reidpinchback8850 10 ай бұрын
@@BboyKeny the part that won't be obvious is that attributes defined on the object (e.g. "def __init()__: self.x = 7") get cached in a different dict than attributes defined on the class ("eg. class Foo: x = 9"). It looks like they are the same attribute, and they almost are the same. The latter provides a default for all instances, but instances can effectively shadow that and have their own value. The two attributes are different entities, but if you do something like "f = Foo() print(f.x)" then the print statement will work in both cases and you have no idea which source of "x" is being used. You can look at Foo.__dict__ and f.__dict__ to see where "x" is and which value it has in either location.
@jeroenvermunt3372
@jeroenvermunt3372 Жыл бұрын
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
@fl3x11
@fl3x11 Жыл бұрын
Is there a reason why meta classes are so much used in the django framework?
@Carberra
@Carberra Жыл бұрын
I don't know enough about Django to be able to answer that, but this might help: medium.com/swlh/how-django-use-data-descriptors-metaclasses-for-data-modelling-14b307280fce
@terrysimons
@terrysimons Жыл бұрын
Using type to create classes would be useful if you wanted to generate your classes dynamically. Polymorphic mutating code, anyone? :)
TypedDict is a LIFESAVER
6:44
Carberra
Рет қаралды 27 М.
Wait, you CAN use braces with Python?!
13:03
Carberra
Рет қаралды 2,9 М.
I PEELED OFF THE CARDBOARD WATERMELON!#asmr
00:56
HAYATAKU はやたく
Рет қаралды 32 МЛН
How did CatNap end up in Luca cartoon?🙀
00:16
LOL
Рет қаралды 7 МЛН
Каха с волосами
01:00
К-Media
Рет қаралды 6 МЛН
Functools is one of the MOST USEFUL Python modules
13:37
Carberra
Рет қаралды 30 М.
Metaclasses in Python
15:45
mCoding
Рет қаралды 147 М.
Modern Python logging
21:32
mCoding
Рет қаралды 142 М.
How To Use Recursion In Python
8:50
Taylor's Software Solutions
Рет қаралды 2,3 М.
Python's magic methods
19:30
Carberra
Рет қаралды 11 М.
Expert Python Tutorial #3 - Metaclasses & How Classes Really Work
20:41
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 785 М.
Cython makes Python INSANELY FAST
19:08
Carberra
Рет қаралды 31 М.
Python dataclasses will save you HOURS, also featuring attrs
8:50
I PEELED OFF THE CARDBOARD WATERMELON!#asmr
00:56
HAYATAKU はやたく
Рет қаралды 32 МЛН