Metaclasses in Python

149,972
0
2021-10-02に共有
Metaclasses customize the class creation process.

We go over what metaclasses are and give some notable examples, including: abstract base classes, adding function overloads, and dataclasses or code generation.

CONTEST WINNERS (CONTEST ENDED 10/9/21) :
PyCharm: Yannick Kuhn, peter duffy
CLion: BalkanSwine, Laurin Neff

OFFICIAL CONTEST RULES:
1. All entries must comply with the YouTube community guidelines ( youtube.com/t/community_guidelines) and YouTube Terms of Service (youtube.com/static?gl=US&template=terms). Entries that violate YouTube guidelines are automatically disqualified.
2. YouTube is not a sponsor of the contest and viewers are required to release YouTube from any liability related to the contest.
3. Privacy notice: no personal data will be collected for this contest.
4. In order to enter, you must (a) be one of my subscribers, AND (b) make a top-level comment to the video including #pycharm or #clion or both. If you only include one you will only have a chance to win that corresponding license.
5. The contest is free, there is no fee required to enter.
6. Winners will be chosen randomly 1 week after the date the video went live from all users who have entered and not been disqualified.
7. Each winner will be notified via a comment reply from me that details what prize was won (e.g. "Congratulations! You have won XYZ. Please email me."). I will ask the winner to contact me by email, and I will reply through email with a random token which must be posted as another reply to the winning comment from the winning account in order to verify account ownership and prevent fraud.
8. Each winner will have 72 hours to respond AND prove account ownership or their prize is automatically forfeited and another winner will be chosen.
9. A winner can only win 1 prize per contest.
10. The prize pool for this contest is: 2 licenses for PyCharm Professional ("Free 1-Year Personal Subscription") and 2 licenses for CLion ("Free 1-Year Personal Subscription"), which are products made by JetBrains. A prize consists of 1 license, which will be delivered in the form of a redeemable code that can be redeemed at www.jetbrains.com/store/redeem/ before May 01, 2022.
11. You may not enter the contest if doing so would be a violation of any relevant federal, state, and local laws, rules, and regulations, including U.S. sanctions.

― mCoding with James Murphy (mcoding.io/)

Source code: github.com/mCodingLLC/VideosSampleCode
Python metaclass docs: docs.python.org/3/reference/datamodel.html#metacla…
Previous video on new:    • __new__ vs __init__ in Python  
Another great metaprogramming video:    • Python 3 Metaprogramming  
Python descriptor docs: docs.python.org/3/howto/descriptor.html
Python ABC docs: docs.python.org/3/library/abc.html

SUPPORT ME ⭐
---------------------------------------------------
Patreon: patreon.com/mCoding
Paypal: www.paypal.com/donate/?hosted_button_id=VJY5SLZ8BJ…
Other donations: mcoding.io/donate

Top patrons and donors: Jameson, Laura M, John Martin, Dragos C, Vahnekie, Pieter G, Casey G, Sigmanificient

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------
Discord: discord.gg/Ye9yJtZQuN
Github: github.com/mCodingLLC/
Reddit: www.reddit.com/r/mCoding/
Facebook: www.facebook.com/james.mcoding

CHAPTERS
---------------------------------------------------
0:00 Intro
1:28 What is a metaclass
3:58 Simplest possible metaclass
4:59 Load time metaclass
6:08 Abstract base classes
8:03 Function overloads
14:10 Dataclass code generation
15:01 Giveaway instruction

コメント (21)
  • @GanerRL
    i will literally never have a practical use case for this but it still makes me happy it exists edit: ok within a year I have used it several times I retract my previous statement
  • Metaclasses looks very powerfull, unfortunately most people using "powerfull" features in a big codebase usually leaves the most powerfull mess to be maintained. Great video! #pycharm
  • My mind is blown. So many possibilities once you know what Python does under the hood. I would find it super interesting to learn more about how C-bindings like numpy uses them are built and how one could add them too. #pycharm
  • @foxcirc
    Since I've lot's of work nothing to do right now, I counted how often you've said the word 'class' and 'metaclass' in this video: · class ⟶ 62 times · metaclass ⟶ 33 times Oh my god, I watched the video on 2x speed and at 1:37, my brain just started to hurt. I really like your content by the way. There are so many useful python features I've hever heard of before. 👍
  • Man the information density in your videos is mindboggling. Every 20 seconds there's something I'd need to sit an hour for to fully absorb the concept and the code. Well done, thanks
  • I feel like your videos have given me a really deep understanding and appreciation of all the internal magic that goes on behind the scenes on Python. Thanks for the content!
  • one of my favorite things about python is how modular the language itself is. You can just overload anything you want, and having the syntax be so malleable is so fun!!
  • I taught Python for 2 years at the university level. I thought I knew more deep wizardry than any person should know. After seeing this, it's clear that I know nothing. #pycharm
  • @dragon_pi
    "the type of big A is a type but the type of small a is big A, whereby the type of type is type" That's exactly the feature you will probably not use for a long time, but years from now you will remember and it will save your life! Best thing about python, it's not like other languages where many base features are hard-coded, but you can customize EVERYTHING #pycharm
  • This is a terrible idea, super slow, and very very dangerous. I absolutely love it.
  • This is actually that rare gem content found under the depths of the internet. Been trying to wrap around meta-classes for a week now, with so little luck. The docs/content revolving around such concepts are so scarce, But you made it so clear as day. Thank you :) don't need pycharm cause VSC is nicer lol
  • I've not used python in years, and primarily use TypeScript myself currently. I had an alright time using Python when I did, but didn't think too much of the language, but your videos including this one really make me appreciate python more! This whole system shown in the video is very elegant imo, and very powerful.
  • It's interesting to see overloading implemented in Python, I saw a stackoverflow comment suggesting to use multiple dispatch instead and now I might see why considering the slowdowns. Great video as always :) #pycharm
  • I've definitely learned more from your channel than any of the other python channels, and courses I've taken combined. Please keep making such great content. #clion
  • So you were trying to overload a function with the wrong syntax and didn't work, the you used the correct way to do it and worked. Mind bending.
  • @_gregor
    Awesome video! It's a bit more advanced than usual, but it's good to hear something like that once in a while!