Тёмный
No video :(

Understanding Python: Metaclasses 

Jake Callahan
Подписаться 2 тыс.
Просмотров 6 тыс.
50% 1

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/Jac...
Timelapse music: 失望した by Eva
Link: • EVA - 失望した [Synthwave]...
Outro music: Elix by Synthness
Link: • Synthness - Elix ★ No ...

Опубликовано:

 

4 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 18   
@dragonkat13
@dragonkat13 Год назад
You're literally the Bob Ross of programming. Amazing.
@armii666
@armii666 Год назад
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 Год назад
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 11 месяцев назад
Instant subscription.
@johnmoff9324
@johnmoff9324 Год назад
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 Год назад
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 Год назад
 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 Год назад
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 Год назад
​@@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 Год назад
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 Год назад
__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.
@blanky_nap
@blanky_nap 5 месяцев назад
Why __new__ of metaclass is a staticmethod? Isn‘t it an implicit classmethod?
@JakeCallahan
@JakeCallahan 5 месяцев назад
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 5 месяцев назад
@@JakeCallahan yep, great channel!!
@relaxingmuzics
@relaxingmuzics 7 месяцев назад
hi, how to run jupyter notebook inside terminal?
@JakeCallahan
@JakeCallahan 7 месяцев назад
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 7 месяцев назад
@@JakeCallahan thank you
Далее
Understanding Python: Abstract Base Classes
21:27
Просмотров 4,6 тыс.
Metaclasses in Python
15:45
Просмотров 153 тыс.
Как дела перцы?
00:25
Просмотров 52 тыс.
Understanding Python: Descriptors
23:42
Просмотров 6 тыс.
Understanding Python: Logging Basics
17:54
Просмотров 737
Understanding Python: Concurrent Coroutines
21:38
Просмотров 1,6 тыс.
Python Data Classes Are AMAZING! Here's Why
16:11
Просмотров 78 тыс.
Python Standard Library: Functools
33:42
Просмотров 1,9 тыс.
Understanding Python: Asyncio
19:43
Просмотров 3,3 тыс.
Modern Python logging
21:32
Просмотров 182 тыс.