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

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

  • @__cmo
    @__cmo 3 ปีที่แล้ว +110

    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.

    • @Tom_Seeker
      @Tom_Seeker 2 ปีที่แล้ว +1

      Still very relevant after 9 years. I've learned Metaclasses basically work the same way. Annotations have changed a lot though.

    • @rdean150
      @rdean150 ปีที่แล้ว

      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.

  • @user-vz3ek6np2s
    @user-vz3ek6np2s 4 หลายเดือนก่อน +2

    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

  • @sakshamgupta5022
    @sakshamgupta5022 ปีที่แล้ว +3

    "This tutorial as you will see in a second aims to combine every horrible insane thing you can possibly have at once." Great tutorial

  • @cronaut5429
    @cronaut5429 5 ปีที่แล้ว +71

    Legitimately the best thing I've seen on TH-cam on Python in my life.

  • @feraudyh
    @feraudyh 9 ปีที่แล้ว +52

    This guy has a way of making it look easy

  • @yashrathi6862
    @yashrathi6862 3 ปีที่แล้ว +5

    It's 2021, still this video is most useful python video on youtube

  • @user-jr4sz3zj1v
    @user-jr4sz3zj1v 7 ปีที่แล้ว +65

    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.

  • @danielguffey
    @danielguffey 4 ปีที่แล้ว +10

    "You can never have enough multiple inheritances, especially with regexes" - David Beazley

  • @rohitbhanot7809
    @rohitbhanot7809 5 ปีที่แล้ว +9

    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 !!

  • @kuolu9247
    @kuolu9247 ปีที่แล้ว

    It's Dec. 2022 and I have been a python programmer for 3 years. I still learned so much from this video.

    • @seaoater
      @seaoater ปีที่แล้ว

      This is my story. I keep coming back to this tutorial everytime. Its too cool.

  • @sanchayana2007
    @sanchayana2007 8 ปีที่แล้ว +50

    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 ... :)

  • @balajimarisetti
    @balajimarisetti 11 ปีที่แล้ว +9

    I never thought python is this powerful. Thanks David for a very good presentation

  • @pawanadhikari110
    @pawanadhikari110 6 ปีที่แล้ว +4

    One of the best presenter, I enjoyed every millisecond of the presentation

  • @corlevtec
    @corlevtec 3 ปีที่แล้ว

    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.

  • @agranero6
    @agranero6 3 ปีที่แล้ว

    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.

  • @mujkocka
    @mujkocka 4 ปีที่แล้ว +2

    Beezley and Hettinger are the best!

  • @Nithael_
    @Nithael_ 3 ปีที่แล้ว +1

    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.

  • @imflash217
    @imflash217 3 ปีที่แล้ว

    Many Pranams 🙏🏽🙏🏽 to Sir David Beazley....... thank you so so much......you are a gem....

  • @derekslone1135
    @derekslone1135 4 ปีที่แล้ว +1

    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!

  • @richardplester
    @richardplester 11 ปีที่แล้ว +1

    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 :)

  • @movax20h
    @movax20h 4 ปีที่แล้ว +2

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

    • @higorhi72
      @higorhi72 ปีที่แล้ว +1

      You just described Pydantic :)

    • @movax20h
      @movax20h ปีที่แล้ว +1

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

  • @vincentmatossian8500
    @vincentmatossian8500 9 ปีที่แล้ว +2

    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!

  • @DeepSukhwani
    @DeepSukhwani 4 ปีที่แล้ว +8

    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.

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

    Wow a python video with actual useful information in it, these are as rare as hen’s teeth these days

  • @cupofstaplers
    @cupofstaplers 3 ปีที่แล้ว

    TH-cam needs more of this.

  • @badoli1074
    @badoli1074 7 ปีที่แล้ว +3

    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.

  • @l_combo
    @l_combo 4 ปีที่แล้ว +1

    Incredible content, would love to see a 1080P version these days.

  • @thedawnofslayer
    @thedawnofslayer 5 ปีที่แล้ว +1

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

    • @justich2567
      @justich2567 5 ปีที่แล้ว

      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

  • @LoganVanCuren
    @LoganVanCuren 11 ปีที่แล้ว +3

    I am surprised that this has so few views! This is really good stuff!

    • @Yankzy
      @Yankzy 2 ปีที่แล้ว

      Coz this is advance, most programmers never make it this far

  • @anikethdeshpande8336
    @anikethdeshpande8336 2 ปีที่แล้ว

    Very few videos go this deep ! Amazing lessons learnt

  • @carlossegura403
    @carlossegura403 4 ปีที่แล้ว

    I learned a lot of new concepts from this video! Thanks, David!

  • @saurabh75prakash
    @saurabh75prakash 5 ปีที่แล้ว

    Never miss Dave's talks if you are a Pythonista.

  • @arngorf
    @arngorf 11 ปีที่แล้ว +1

    This is madness. I like it. I feel dirty learning these tricks!.. or perhaps just a bit like a magician.

  • @daiharr
    @daiharr 8 ปีที่แล้ว +3

    David Beazley - The MetaSatan!!!

  • @phivos123
    @phivos123 8 ปีที่แล้ว +32

    "if you do everything in this tutorial all at once you will either be fired or have permanent job security" !!! :‑D

  • @bionikspoon
    @bionikspoon 9 ปีที่แล้ว +1

    annotations could be used for dependency injection with an IOC container. Like PHP's Laravel or Angular.

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

    Awesome tutorial

  • @verfran
    @verfran 3 ปีที่แล้ว

    wow wow wow.. great stuff. thank you

  • @tmst2199
    @tmst2199 7 ปีที่แล้ว +9

    The only thing I can imagine topping this is seeing Guido write the C Python implementation.

  • @EvanOfTheDarkness
    @EvanOfTheDarkness 4 ปีที่แล้ว +5

    "The only real way to debug is with print..." - That my guy!

  • @pward17
    @pward17 6 ปีที่แล้ว +1

    I feel enlightened.

  • @snail8720
    @snail8720 7 ปีที่แล้ว +1

    good lecturer

  • @pinkstaffp
    @pinkstaffp 11 ปีที่แล้ว

    Great class. Now I have lots to keep me busy.

  • @StudentOfKyoto
    @StudentOfKyoto 10 ปีที่แล้ว

    superb presentation

  • @zakaria.morchid
    @zakaria.morchid 3 ปีที่แล้ว

    I kinda enjoy this tutorial, i spend two day to finish it, it worth spend time watching it, and use it like reference, Thanks

  • @taiman9423
    @taiman9423 9 วันที่ผ่านมา

    jawdropping

  • @KrishnaDasPC
    @KrishnaDasPC 2 ปีที่แล้ว

    Brilliant 👏

  • @hosseindehghanpoor836
    @hosseindehghanpoor836 2 ปีที่แล้ว

    fabelous 👌

  • @SaniSensei
    @SaniSensei 9 ปีที่แล้ว +12

    My mind was fu*ked

  • @RounakJainIs
    @RounakJainIs 6 ปีที่แล้ว

    Too Good! Cannot thank you enough!

  • @The0ubik
    @The0ubik 3 ปีที่แล้ว

    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"

  • @MeSuAx
    @MeSuAx 11 ปีที่แล้ว +4

    This is very interesting, thanks. But why on earth are you not using IPython or some other shell replacement?

  • @ThankYouESM
    @ThankYouESM 11 ปีที่แล้ว +1

    Fully understood

  • @mnfchen
    @mnfchen 8 ปีที่แล้ว +2

    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?

    • @movax20h
      @movax20h 4 ปีที่แล้ว

      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.

  • @trisinogy
    @trisinogy 2 ปีที่แล้ว

    Maybe I missed it, but does generating code from XML improve the performance in any way? The presenter didn't mention it, I believe.

  • @samverstocken
    @samverstocken 7 ปีที่แล้ว

    Great talk!

  • @hyperboogie
    @hyperboogie 6 ปีที่แล้ว +4

    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!)

  • @lennonfernandes1696
    @lennonfernandes1696 4 ปีที่แล้ว

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

  • @cestlacroix
    @cestlacroix ปีที่แล้ว

    great

  • @gdhameeja
    @gdhameeja 6 ปีที่แล้ว

    its 2017, I thought I knew python. Blown

  • @thedawnofslayer
    @thedawnofslayer 5 ปีที่แล้ว

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

  • @agumonkey
    @agumonkey 11 ปีที่แล้ว +1

    Came here to say that. Maybe in ten years the world will be ready to switch to 90s technology.

  • @Ca1vema
    @Ca1vema ปีที่แล้ว

    1:39:00 "it's so bad, I'm gonna burn in he'll for that"
    @
    Data classes made their way in python
    😂

  • @Agrover112
    @Agrover112 3 ปีที่แล้ว

    Wow I have found a gem

  • @hangzhou204
    @hangzhou204 4 ปีที่แล้ว +2

    He sounds like a intelligent Peter Griffith in some way

  • @thedawnofslayer
    @thedawnofslayer 5 ปีที่แล้ว

    @4:48 Maybe you have forgotten `nonlocal` keyword that works on the nearest scope.

  • @DrCshell
    @DrCshell 11 ปีที่แล้ว

    But... everyone is eager to show theirs off to their peers... so... take what you will from that. :)

  • @senyai
    @senyai 11 ปีที่แล้ว

    Nice code highlighting. Now I want to import xls files for no reason >:)

  • @undercoversuit9475
    @undercoversuit9475 4 ปีที่แล้ว

    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 😁

  • @allesmedvesek
    @allesmedvesek 5 ปีที่แล้ว

    I am quite sure some jokes flew right over my head.

  • @coolzilj
    @coolzilj 8 ปีที่แล้ว

    best ever seen

  • @merajahmed4434
    @merajahmed4434 3 ปีที่แล้ว

    Wow. 7 years old.

  • @hermesmercuriustrismegistu4841
    @hermesmercuriustrismegistu4841 4 ปีที่แล้ว

    is there is a way to get the slides and code of this lovely lecture?

  • @maybealover
    @maybealover 10 ปีที่แล้ว

    Honoring Kundalini

  • @viktorlomakin4967
    @viktorlomakin4967 7 ปีที่แล้ว

    Cool!

  • @vishcdac07
    @vishcdac07 9 ปีที่แล้ว

    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?

    • @vishcdac07
      @vishcdac07 9 ปีที่แล้ว

      Oops! In debug decorator, I forgot to specify prefix argument in **kwargs format. Now works fine.

    • @sohamdats
      @sohamdats 8 ปีที่แล้ว

      yes you are right. because I think when the prefix keyword is omitted the wrapper function takes '***' as the function to be wrapped.

  • @c2oo7
    @c2oo7 11 ปีที่แล้ว

    great!

  • @stackoverflow8260
    @stackoverflow8260 ปีที่แล้ว

    Can anyone confirm if help() is wrapping output if inspect() nowadays?

  • @cnliving
    @cnliving 8 ปีที่แล้ว

    Good!

  • @world-traveler880
    @world-traveler880 9 ปีที่แล้ว

    Why doesn' the __mro__ order he calls at 1:20:20 match the __mro__ list in the next slide?

    • @TPHRyan
      @TPHRyan 9 ปีที่แล้ว

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

  • @ibraklay9343
    @ibraklay9343 11 ปีที่แล้ว

    jada killed this shit

  • @meallanmdl
    @meallanmdl 11 ปีที่แล้ว

    awsome

  • @angelcaru
    @angelcaru 4 ปีที่แล้ว

    Debug All Methods > Debug All Classes > Debug All Universes > Debug All Multiverses > Debug The Crash Of The Python Console

  • @MeSuAx
    @MeSuAx 11 ปีที่แล้ว

    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.

  • @DarkSun1988
    @DarkSun1988 6 ปีที่แล้ว +1

    Please, the font's name!

  • @fandibataineh4586
    @fandibataineh4586 2 ปีที่แล้ว

    my brain!!

  • @yomajo
    @yomajo 4 ปีที่แล้ว

    Sweet mother of god...

  • @Vegetarianrage
    @Vegetarianrage 11 ปีที่แล้ว

    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.

  • @keshavnemeli
    @keshavnemeli 4 ปีที่แล้ว

    Does anybody have the code used in this? Please post a github link. Thank you.

  • @yuriy1950
    @yuriy1950 6 ปีที่แล้ว +4

    All is true except one: all metaprogramming should be done in vim.

  • @amnotgenjimain6027
    @amnotgenjimain6027 3 ปีที่แล้ว

    Don't mind this comment, I'm just time stamping for myself
    9:38

  • @kutilkol
    @kutilkol 3 ปีที่แล้ว

    Is this still actual in 2021?

  • @Justinba1010
    @Justinba1010 11 ปีที่แล้ว +1

    This isn't Black Hat Hacking, this is just hacking, also this has nothing to do with web hacking(TCP-IP)

  • @farainechikwira3592
    @farainechikwira3592 4 ปีที่แล้ว

    When you think you are some python wiz and DaBeaz shuts u up

  • @vanillakeksik
    @vanillakeksik 2 ปีที่แล้ว +1

    damn

  • @MatthewRalston89
    @MatthewRalston89 7 ปีที่แล้ว +1

    To clarify from the audio, closure != function that returns a function. That was a horrible definition of a closure.

    • @tmst2199
      @tmst2199 7 ปีที่แล้ว +1

      Or at least an incorrect one...

  • @AlexanderPetersonml
    @AlexanderPetersonml 9 ปีที่แล้ว +6

    tfw my company uses 2.7

    • @Ca1vema
      @Ca1vema ปีที่แล้ว +1

      Tfw my company still uses 2.7 and its 2023

  • @user-vz3ek6np2s
    @user-vz3ek6np2s 4 หลายเดือนก่อน

    Seems like he left you tube also , he posted his last video before 4 years.

  • @GPCTM
    @GPCTM 8 ปีที่แล้ว +13

    I've hit the 0.5 X speed and it became more interesting.

  • @PabloBalerio
    @PabloBalerio 7 ปีที่แล้ว +1

    I can never get past minute 8th.