Understanding Python: Metaclasses

Sdílet
Vložit
  • čas přidán 11. 09. 2024
  • 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 ...

Komentáře • 18

  • @dragonkat13
    @dragonkat13 Před rokem +5

    You're literally the Bob Ross of programming. Amazing.

  • @armii666
    @armii666 Před rokem

    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  Před rokem

      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 Před 11 měsíci

    Instant subscription.

  • @ingydegmar2060
    @ingydegmar2060 Před rokem

    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  Před rokem +2

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

  • @johnmoff9324
    @johnmoff9324 Před rokem

    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  Před rokem

      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 Před rokem +1

       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  Před rokem

      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 Před rokem

      ​@@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)

  • @blanky_nap
    @blanky_nap Před 6 měsíci

    Why __new__ of metaclass is a staticmethod? Isn‘t it an implicit classmethod?

    • @JakeCallahan
      @JakeCallahan  Před 6 měsíci +1

      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 Před 6 měsíci

      @@JakeCallahan yep, great channel!!

  • @relaxingmuzics
    @relaxingmuzics Před 7 měsíci

    hi, how to run jupyter notebook inside terminal?

    • @JakeCallahan
      @JakeCallahan  Před 7 měsíci

      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 Před 7 měsíci

      @@JakeCallahan thank you