Understanding Python: Metaclasses

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

Jake Callahan

Jake Callahan

Жыл бұрын

In this video, I go over how to create and use metaclasses.
Included in the lesson are a dive into how classes are constructed, a demonstration of four metaclass magic methods, and the creation of two custom metaclasses.
As always, if you have any questions or suggestions for future videos, please leave a comment down below.
Follow me on Twitter: / jakejcallahan
Source: github.com/JacobCallahan/Unde...
Timelapse music: 失望した by Eva
Link: • EVA - 失望した [Synthwave]...
Outro music: Elix by Synthness
Link: • Synthness - Elix ★ No ...

Пікірлер: 18
@dragonkat13
@dragonkat13 Жыл бұрын
You're literally the Bob Ross of programming. Amazing.
@armii666
@armii666 10 ай бұрын
this video doesn't deserve this amount of views, why are there so few of them??? thank you for making it clean for me!
@JakeCallahan
@JakeCallahan 10 ай бұрын
Thank you and please let me know if there is a topic you'd like me to make a video on. As for my release cadence, my job keeps me quite busy so I'm currently releasing one video a month.
@viktoryurchenko756
@viktoryurchenko756 7 ай бұрын
Instant subscription.
@johnmoff9324
@johnmoff9324 10 ай бұрын
Wow! Just wow! You are a god of python. Wow! Wonderful explanation. In your example of GoodChild/BadChild the error was thrown when the class was being created so your metaclass only focused on the __new__ method, this got me wondering, I mean for abstract methods will the check be in the __call__ method instead of __new__? Cause when parent class has abstract methods the child class can still be created even when the abstract method is not defined and only throws error when creating an object of the child class
@JakeCallahan
@JakeCallahan 10 ай бұрын
Thank you, you're much too kind. That's a fantastic question, bringing together those two pieces! For abstract methods, that happens in __new__ which happens after __call__. With that in mind, you may be able to exploit that behavior for even deeper checks than just abstract methods. Here's a quick gist if you wanted to play around with your idea. It is a modification of the NoisyMeta class. gist.github.com/JacobCallahan/c81e4d7572b2007372a77608e00899f8
@johnmoff9324
@johnmoff9324 10 ай бұрын
 ah I probably missed that part, so the __new__ method is also called when an instance is created not just when the class is created?
@JakeCallahan
@JakeCallahan 10 ай бұрын
Be careful to remember which __new__ is being called because there are 2. The first is the one on the class' metaclass that created the class object. The second is the one on the class object that creates the class instance. In your question, __call__ is ran after the first (because the class object must exist before it can be called) but before the second. __call__ -> __new__ -> __init__
@johnmoff9324
@johnmoff9324 10 ай бұрын
​@@JakeCallahan sorry I'm a lil bit confused, when we create the class MyClass with meta class MyMetaClass the following method will be executed : __prepare__(method from MyMetaClass) -> __new__(method from MyMetaClass) -> (execute MyClass body) -> __init__(method from MyMetaClass) all these methods are from the metaclass MyMetaClass is this correct? Then when we create instance of MyClass such that my_class_instance = MyClass() the following will happen: __call__(method from the MyMetaClass) -> __new__(method from MyClass) -> __init__(method from MyClass)
@ingydegmar2060
@ingydegmar2060 11 ай бұрын
During example3 you created an instance using Example instead of Example3. Example3 works, but didn't do the __new__ part. Added it in with the (Example3,cls) included like you did in Example2. What does __new__ do exactly? I thought all classes started with __init__.
@JakeCallahan
@JakeCallahan 11 ай бұрын
__new__ creates the class instance that is then passed in to __init__. This instance object is what __init__ accepts most commonly as "self". You're correct that in the third example, I didn't define a __new__ method, so it used Python's default implementation.
@relaxingmuzics
@relaxingmuzics 3 ай бұрын
hi, how to run jupyter notebook inside terminal?
@JakeCallahan
@JakeCallahan 3 ай бұрын
While not a full notebook, I Python is what Jupyter uses under the hood. You can do the same as you see me doing by installing IPython with pip, then running from your terminal. pip install IPython Then the command is just: ipython
@relaxingmuzics
@relaxingmuzics 3 ай бұрын
@@JakeCallahan thank you
@blanky_nap
@blanky_nap 2 ай бұрын
Why __new__ of metaclass is a staticmethod? Isn‘t it an implicit classmethod?
@JakeCallahan
@JakeCallahan 2 ай бұрын
Great catch! The reason this still works, and wouldn't if the inverse were true, is that Python is passing the meta class as the first positional argument. So while this is decorated with staticmethod, there is no real effect on the behavior since Python itself is the consumer. We can verify this by looking at the output at 18:09
@blanky_nap
@blanky_nap 2 ай бұрын
@@JakeCallahan yep, great channel!!
Understanding Python: Abstract Base Classes
21:27
Jake Callahan
Рет қаралды 3,5 М.
Understanding Python: Descriptors
23:42
Jake Callahan
Рет қаралды 5 М.
Kitten has a slime in her diaper?! 🙀 #cat #kitten #cute
00:28
Когда на улице Маябрь 😈 #марьяна #шортс
00:17
Metaclasses in Python
15:45
mCoding
Рет қаралды 147 М.
Python Metaclasses: Everything is an Object
31:23
Real Python
Рет қаралды 6 М.
Modern Python logging
21:32
mCoding
Рет қаралды 143 М.
Understanding Python: Threading
23:26
Jake Callahan
Рет қаралды 1,3 М.
Understanding Python: Concurrent Coroutines
21:38
Jake Callahan
Рет қаралды 1 М.
Understanding Python: Multiprocessing
21:50
Jake Callahan
Рет қаралды 2,7 М.
Python Generators
15:32
mCoding
Рет қаралды 127 М.
Extending Python's Number hierarchy
16:11
mCoding
Рет қаралды 28 М.
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 785 М.
СЛОМАЛСЯ ПК ЗА 2000$🤬
0:59
Корнеич
Рет қаралды 2,5 МЛН
phone charge game #viral #tranding #new #reels
0:18
YODHA GAMING RAAS
Рет қаралды 12 МЛН
🤖Вернулись в ПРОШЛОЕ🤪
0:28
Demin's Lounge
Рет қаралды 67 М.
Apple, как вас уделал Тюменский бренд CaseGuru? Конец удивил #caseguru #кейсгуру #наушники
0:54
CaseGuru / Наушники / Пылесосы / Смарт-часы /
Рет қаралды 4 МЛН