Are metaclasses the HARDEST thing in Python?

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 พ.ย. 2024

ความคิดเห็น • 25

  • @Carberra
    @Carberra  หลายเดือนก่อน

    Head to ultahost.com to host your website or application for less!

  • @yaroslavdon
    @yaroslavdon หลายเดือนก่อน +3

    In my experience, meta-classes are used to change class behaviour and to better encapsulate said behaviour. The two prominent examples are ABCMeta and EnumMeta (from abc and enum packages, respectively). ABCMeta makes a class, well, abstract. It also adds useful features (like recognizing @abstractmethod) and handles virtual superclass registry. EnumMeta is much more complex, as it needs to configure the full behaviour of an Enum (which is a very special class). For example, it needs to properly describe inheritance action, the way to pickle items, etc. I suggest reading these two metaclasses' code; it teaches a lot.
    I've had another-rather unusual-usage of metaclasses is for type annotations. I've used helper functions that accept a general enum class as an attribute (to iterate over the enum). The enum's type is EnumMeta.

    • @Carberra
      @Carberra  หลายเดือนก่อน +2

      I'll have a look at those sources, thanks for the suggestion!

  • @Dht1kna
    @Dht1kna หลายเดือนก่อน +1

    Idk perhaps CAPI extensions to overcome GIL bottlenecks sound much harder, reflection stuff with code-gen might be harder too. Customizing Garbage collection stuff, heck even effectively using weakrefs by itself, are harder too imo

  • @VivekKumarSinha25
    @VivekKumarSinha25 หลายเดือนก่อน +1

    5:40 the class you created using "class" keyword is not the same as one you created using type, although it may appear to be, with only difference being, name, age and job are class level attributes when created using "type" built-in, meaning all instance of Profile will share those attributes with same values. While in 2nd case, each object needs to initialized with those parameters, since no defaults have been specified. It's one difference, but a big one. EDIT: thanks for clarifying it later 8:00 +

    • @Carberra
      @Carberra  หลายเดือนก่อน +1

      Yeah I noticed it in editing and thought I should correct myself lmao.

  • @blanky_nap
    @blanky_nap หลายเดือนก่อน +3

    Metaclasses are probably less spoken topic in python, but by far not one of the most complicated subjects

  • @marckiezeender
    @marckiezeender หลายเดือนก่อน

    13:50 There is a problem with your singleton base class: the dunder init method in the subclass gets run every time you call DatabaseManager, in this case twice, which probably isn't intended behavior, especially if that method connects twice to a database. I guess a metaclass is better after all!
    Also, it's possible to make an actual classproperty using a descriptor (although it won't be able to override assignment)

  • @maleldil1
    @maleldil1 หลายเดือนก่อน +2

    I don't quite understand when it's appropriate to use them. Also, I wish you'd covered ABCMeta vs ABC. I usually go for ABC, but when should ABCMeta be used instead?

    • @Carberra
      @Carberra  หลายเดือนก่อน +3

      Setting the metaclass to ABCMeta is _slightly_ faster than inheriting from ABC. That's the only difference.

    • @legion_prex3650
      @legion_prex3650 หลายเดือนก่อน +1

      Image you have to build some kind of framework for your coworkers. They have to use some of your classes. And you wanna make sure, they use the classes the proper way. For example, they have to define a string, which is a path to a Json-File. You can now check in the metaClass, that they indeed used the proper file-suffix in that string. Stuff like that. You will probably not need it for your own code. Python itself is packed with Metaclasses.

    • @jlp2011
      @jlp2011 หลายเดือนก่อน +1

      They are, more than a bit, deep black magic. However, if you want to configure attributes at the class, not instance, level, they can be very powerful, esp when there are relationships between class attributes. Let’s say you have an RPG with warrior classes. A TwohandedSwordFighter class could be a specialized class that takes 1 and only 1 sword in its init (using kwargs and super calls). While a ShieldFighter would take one weapon and 1 shield. You could configure that behavior with mixins and a class-level weapons attribute that is “merged” by metaclassing. Is it the best way to model this? Not necessarily but it is one possible approach.

  • @stharding1
    @stharding1 หลายเดือนก่อน +1

    I've used metaclasses professionally several times over the years. This is the best deep dive on the topic I am aware of: th-cam.com/video/sPiWg5jSoZI/w-d-xo.html

  • @k283
    @k283 หลายเดือนก่อน +4

    It's wrong at 7:00: a class object is created during ~~new~~ of the metaclass, not during ~~init~~. By the time metaclass' ~~init~~ runs, a class object already exists.
    So in metaclasses, ~~init~~ is more like a class decorator, which is applied to subclasses as well (because they share the same metaclass)
    Sadly, this video has only some shallow basics and syntax, not really a deep dive. You didn't explain how metaclasses work - only some syntax on how to use them...

    • @AhmedIsam
      @AhmedIsam หลายเดือนก่อน

      Don't even look for a deeper dive, classes are rubbish. Do OOP paradigm and save yourself unnecessary abstractions.

  • @lostsmile6792
    @lostsmile6792 หลายเดือนก่อน

    Why do your timestamps have wrong ordering?
    It starts with "Intro [1]", which is wrong... it should have been "Intro [0]"

    • @Carberra
      @Carberra  หลายเดือนก่อน

      Not sure if this is a joke about zero-indexing or not haha. In case it isn't, the [1] matches up to the first reference, listed below the chapters.

  • @OmidAtaollahi
    @OmidAtaollahi 6 วันที่ผ่านมา

    too complicated that I didn't understand😮

  • @richardbennett4365
    @richardbennett4365 หลายเดือนก่อน

    Definitely 😮😂 must be the hardest thing to teach, too.
    Did anyone understand the presentation?
    He forgot to explain the structure and element and instead just wrote an example of how these structures and elements are invoked.
    His audience must be for those who already understand these metaclasses.
    Then, in the middle the presenter said he did something differently from how he meant to do something?
    Why wouldn't he redo his presentation so the wrong part is not still in there?
    Why?

  • @capnmnemo
    @capnmnemo หลายเดือนก่อน +1

    Spelling.
    Spelling is the "trickest" thing.

    • @Carberra
      @Carberra  หลายเดือนก่อน +1

      Good job the thumbnail is AB tested so only half the viewers saw that lmao.

    • @xj0ex39
      @xj0ex39 หลายเดือนก่อน

      Trickiest ***

    • @Carberra
      @Carberra  หลายเดือนก่อน

      This has been fixed, thanks for bringing it to my attention!

    • @capnmnemo
      @capnmnemo หลายเดือนก่อน

      Someone is wrong on the Internet is the strongest engagement factor.

    • @Carberra
      @Carberra  หลายเดือนก่อน

      Literally yeah. Though I don't like to be wrong on purpose!