Python 3 Metaprogramming
ฝัง
- เผยแพร่เมื่อ 21 ส.ค. 2024
- David Beazley
Some of the most significant changes in Python 3 are related to metaprogramming. In this tutorial, I'll cover decorators, class decorators, descriptors, and metaclasses. However, the focus will be on idioms and examples that are only
Anyone coming across this video and hesitant that it's xyz years old. Do not worry! I'm watching this 8 years after publish and this lecture is still insanely informative, and touches on things that are so core they will likely be relevant in python for years to come still.
Still very relevant after 9 years. I've learned Metaclasses basically work the same way. Annotations have changed a lot though.
Agreed. The people in the audience were bringing up the exact points I was thinking too, during the metaclass discussion, such as the order of execution of metaclasses vs class decorators.
But what's craziest about this for me is that I actually attended this PyCon back in 2013 but didn't attend this session. I was only a couple of years into my Python journey at the time though, and my team was still on 2.7 back then, so I don't think I was ready for this at the time anyway. Fast forward to today, and I've just spent the past couple of months writing a library that heavily utilizes descriptors, and even involves a couple of custom metaclasses. So this video feels like a weird time travel lesson for me.
Watched this video today in 2024 April, 😄He is pro in meta class before 11 years, The one concept which is very help in programming , i got to know today after working 5 years in ptyhon
"This tutorial as you will see in a second aims to combine every horrible insane thing you can possibly have at once." Great tutorial
Legitimately the best thing I've seen on TH-cam on Python in my life.
This guy has a way of making it look easy
It's 2021, still this video is most useful python video on youtube
Hello, for the first time in my life, I've actually logged in to write a comment on a TH-cam video. All I can say is that I've learned so much from this video and the quality of contents in this video is amazing. Thank you so much! You are awesome.
So true,
"You can never have enough multiple inheritances, especially with regexes" - David Beazley
He surely is by far the best speaker or instructor we have in python world, every time i watch any of his videos he simply blows your mind away.. I have just watched first 40 mins of this talk and he has shown stuff you wouldn't even imagine in your dreams .. Great stuff !!
It's Dec. 2022 and I have been a python programmer for 3 years. I still learned so much from this video.
This is my story. I keep coming back to this tutorial everytime. Its too cool.
Sir David Beazley , I blown out by the style of your presentation and the way you handle complexities , Truelly Impressed by the Phrases you used to make it constanly interesting . The Attendees throughly enjoyed ... :)
I never thought python is this powerful. Thanks David for a very good presentation
One of the best presenter, I enjoyed every millisecond of the presentation
There's just a word that comes to my mind every time I watch this: Addictive!
Like once you start watching you just can't stop.
Talks from him are always a pleasure. What he says is as important as what he insinuate. He remembers me of those magicians that pretend that that trick went south and then ... voilá ... it is better than you thought it could be.
Beezley and Hettinger are the best!
This video is simply awesome. I was immediately able to solve a series of problems I have been working on for days. It’s not that the information isn’t out there, it’s that his examples are so good.
Many Pranams 🙏🏽🙏🏽 to Sir David Beazley....... thank you so so much......you are a gem....
I've been wondering about Python internals for some time - this is just loverly. Great knowledge and beutiful sarcasm!! And David is a *firm* believer in DRY. Thank you so much!
David's a great educator-I like the pace and the humour. As a noob to python I landed on this vid by accident and despite it not being not relevant to what I'm studying I ended up watching it all, I guess locking the doors at the back worked ;) Tempted to go get 3.3 and start playing :)
The __annotations__ stuff is the most cool thing I have seen in the long time. Combined with metaclasses on a module or class level, and custom __prepare__ to use ordered dict that captures all assignments, not just the last one, you then do some transparent wrapping, or using decorators, and return a proxy object, that inspects all signatures in annotations, and selects the one you want based on the runtime call conditions, like number and types of arguments. I am writing a VM simulator, and I think I will use this technique to implement the opcode encoding in the custom assembler, which can have many different forms depending on the types and sizes of arguments, etc. Not only that, it can be used to automatically create documentation or optimize the encoding globally.
These techniques can be used in few other languages, including some statically typed ones, like D language, but Python one does have few extra tricks available because of how you can capture all these dicts and do own stuff with it. (It can be done in D language too, but using a bit different techniques, including traits, compile time introspection, string and template mixins, and template methods).
You just described Pydantic :)
@@higorhi72 hehe. Indeed. I discovered pydantic few months ago, and started using it for some production system configs at work. Pretty neat.
I also wrote my own system as described above, for VM / ASM, for emulation and electronics work, based on similar principles. It is quite powerful and neat.
Great talk, thank you! I was however a bit surprised to hear that all the dynamic code generation to overcome performance issues related to walking the __mro__ did not qualify as "hacks" around limitations of Python. With that said, this is a really inspiring talk, it really elevates curiosity and interest in Python!
It is weirdly interesting to see the video from 1:45 minute mark onwards, when David starts to get into literal code generation like `_make_init`, `set_code`, and then convincing the manager slide, and then generating the code literally from XML 😂 Yeah he really does that. I mean your brain says - why am I still watching this? Your heart says - its fun, let us continue 😁
I am currently on 2:08:10 mark, let's see where this roller coaster ride goes. On the serious side, there is way too much to learn if you are interested in just learning.
I was telling about this to one of my colleagues the other day at work and we are into a web-development job (Python/Django stuff) and he asked where would I use this? My answer to him "I don't know", I am enjoying learning some really cool stuff, maybe someday if I am aware of these tools I have in my belt when the opportunity comes, I will flex one of these tools but as of now, it's just that I enjoy learning.
Wow a python video with actual useful information in it, these are as rare as hen’s teeth these days
TH-cam needs more of this.
Love that guy. And i'm already amused about how far this guy is in front of me. I'm a sorta python car mechanic and he's teaching me how to build factories to create engines... Mind blown.
Incredible content, would love to see a 1080P version these days.
@7:47
That topic blows my mind too because of the name of the class.
The "base class" has so many names: superclass, parent, and ancestors class are very known.
The "foo" (subclass) is also known as a child, and heir (maybe have more, but I don't remember right now.)
i hope you get it throught
i know it is hard to understand but time will tell
yiu can read
"python Notes for professionals" and after this book go read effective python then python cookbook
good luck
I am surprised that this has so few views! This is really good stuff!
Coz this is advance, most programmers never make it this far
Very few videos go this deep ! Amazing lessons learnt
I learned a lot of new concepts from this video! Thanks, David!
Never miss Dave's talks if you are a Pythonista.
This is madness. I like it. I feel dirty learning these tricks!.. or perhaps just a bit like a magician.
David Beazley - The MetaSatan!!!
"if you do everything in this tutorial all at once you will either be fired or have permanent job security" !!! :‑D
annotations could be used for dependency injection with an IOC container. Like PHP's Laravel or Angular.
Awesome tutorial
wow wow wow.. great stuff. thank you
The only thing I can imagine topping this is seeing Guido write the C Python implementation.
"The only real way to debug is with print..." - That my guy!
I feel enlightened.
good lecturer
Great class. Now I have lots to keep me busy.
superb presentation
I kinda enjoy this tutorial, i spend two day to finish it, it worth spend time watching it, and use it like reference, Thanks
jawdropping
Brilliant 👏
fabelous 👌
My mind was fu*ked
Fall back to your metamind
Too Good! Cannot thank you enough!
Start at th-cam.com/video/sPiWg5jSoZI/w-d-xo.html - "...everything has a type - there's a type system - it's in the language"
This is very interesting, thanks. But why on earth are you not using IPython or some other shell replacement?
Fully understood
So in the end, metaclasses are slower than just using classes. But, does the slowdown only affects class instantiation, or does it (also) affect runtime performance?
Actually that is not true. metaclasses provide zero performance impact compare to classes. They are only used during creation of the class, not creation of the instance. The performance would be very similar if you would write the same functionality manually. He is not comparing apples to apples there.
Maybe I missed it, but does generating code from XML improve the performance in any way? The presenter didn't mention it, I believe.
Great talk!
My brain just raised a BeasleyAwsomenessException
I like Hetinger a lot but I *WORSHIP* Beasley.
He should probably change his last name to Beastly (and he would probably do it with a decorator!)
Sir, I have a doubt.. In the "Reformulation " slide at 1:51:15, why is there a need to use "@staticmethod" above the "set code" function?
Will the code not work without using "@staticmethod" ??
We can just use "def set_code" and continue na, without using "@staticmethod"...
great
its 2017, I thought I knew python. Blown
@7:37
1. I don't understand why "ALMOST everything can be customized." Are you mean about "immutable and mutable" data?
2. Every time that I see someone talking about "special methods" my mind blows off because I've many flashbacks on it, mainly name conventions: dunder ([d]ouble [under]score), _var & var_ (best practice only), and __variable that have a side effect in the interpreter.
Came here to say that. Maybe in ten years the world will be ready to switch to 90s technology.
1:39:00 "it's so bad, I'm gonna burn in he'll for that"
@
Data classes made their way in python
😂
Wow I have found a gem
He sounds like a intelligent Peter Griffith in some way
@4:48 Maybe you have forgotten `nonlocal` keyword that works on the nearest scope.
But... everyone is eager to show theirs off to their peers... so... take what you will from that. :)
Nice code highlighting. Now I want to import xls files for no reason >:)
imho David Beazley sounds like how I imagine William Osman would sound like if he would have gotten into 1 thing instead of being random and just screwing around 😁
I am quite sure some jokes flew right over my head.
best ever seen
Wow. 7 years old.
is there is a way to get the slides and code of this lovely lecture?
Honoring Kundalini
Cool!
Somehow when I try to use the decorator @ 19:18 it gives below error:
...
msg = prefix + func.__qualname__
AttributeError: AttributeError: 'str' object has no attribute '__qualname__'
Seems like it is treating func not as callable but as str. Any idea what's wrong?
Oops! In debug decorator, I forgot to specify prefix argument in **kwargs format. Now works fine.
yes you are right. because I think when the prefix keyword is omitted the wrapper function takes '***' as the function to be wrapped.
great!
Can anyone confirm if help() is wrapping output if inspect() nowadays?
Good!
Why doesn' the __mro__ order he calls at 1:20:20 match the __mro__ list in the next slide?
+growingfuzzybeards You might notice the failed example for the PositiveInteger is still on screen - In the slides, Positive and Integer were not swapped in the class definition, but in his code, they were.
jada killed this shit
awsome
Debug All Methods > Debug All Classes > Debug All Universes > Debug All Multiverses > Debug The Crash Of The Python Console
Yeah, but it's more related to the demo. It's much easier if you can recall your previous commands and use autocompletion for faster typing.
Please, the font's name!
my brain!!
Sweet mother of god...
That's great if what you want to do is easily created by gluing together ready-made parts. This is more of an exploration of what is possible in the language. Someone has to write your code generator, after all.
Does anybody have the code used in this? Please post a github link. Thank you.
All is true except one: all metaprogramming should be done in vim.
Don't mind this comment, I'm just time stamping for myself
9:38
Is this still actual in 2021?
This isn't Black Hat Hacking, this is just hacking, also this has nothing to do with web hacking(TCP-IP)
When you think you are some python wiz and DaBeaz shuts u up
damn
To clarify from the audio, closure != function that returns a function. That was a horrible definition of a closure.
Or at least an incorrect one...
tfw my company uses 2.7
Tfw my company still uses 2.7 and its 2023
Seems like he left you tube also , he posted his last video before 4 years.
I've hit the 0.5 X speed and it became more interesting.
I can never get past minute 8th.