Metaclasses in Python

  Рет қаралды 158,558

mCoding

mCoding

Күн бұрын

Пікірлер: 829
@Ganerrr
@Ganerrr 3 жыл бұрын
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
@tswdev
@tswdev 3 жыл бұрын
The answer is basically proper encapsulation. You can shift the complexity to the caller (outside the class) or you can keep it in the class where its manageable while keeping the caller (outside the class) simple. Creativity is everything and this is just another tool to support that.
@Jason-b9t
@Jason-b9t 3 жыл бұрын
I actually used Metaclass to modify the __iter__ method of my Enum class to skip some values. Basically like this: from enum import Enum, EnumMeta, auto class MyEnumMeta(EnumMeta): def ignore(Cls, e): return False def __iter__(Cls): return (e for e in super().__iter__() if not Cls.ignore(e)) class MyState(Enum, metaclass=MyEnumMeta): Empty = auto() A = auto() B = auto() C = auto() @classmethod def ignore(Cls, e): return e is Cls.Empty
@skully90
@skully90 3 жыл бұрын
I used it all the time when I was a Java developer when I needed similar blocks of code to handle single item of type X or a list of items of type X. 99% of the time it was two functions with one function handling single item and the "overloaded" 2nd function handling the List of that item
@tochka832
@tochka832 3 жыл бұрын
dunno, i use overloads all the time in Nim and C++, its rather handy tool for segmenting code otherwise you would need to create a single function in python that has all the implementations depending on args* kwargs** signature which is rather clunky
@tytywuu
@tytywuu 3 жыл бұрын
@@Jason-b9t crazy
@andrewglick6279
@andrewglick6279 3 жыл бұрын
This video was so magical I'm tempted to call it a ___video___
@qkimat
@qkimat 3 жыл бұрын
Or a video of a __video__
@zoulei881027
@zoulei881027 3 жыл бұрын
5 letters are too long. use __vid__
@30IYouTube
@30IYouTube 3 жыл бұрын
Ah... *dunder video*
@boggri
@boggri 3 жыл бұрын
magic, like a magic ___methods___ in Python )))
@tips1483
@tips1483 2 жыл бұрын
@@zoulei881027 yeah and the __str__ will return “video”
@QuantumHistorian
@QuantumHistorian 3 жыл бұрын
7:55: _"No need to be confused"_ Me: Most confused I've ever been in an mCoding video #pycharm
@pericofalcor
@pericofalcor 3 жыл бұрын
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
@5h5hz
@5h5hz 3 жыл бұрын
Hello! "Powerful" is spelt with 1 'l' not 2 :)
@royz_1
@royz_1 3 жыл бұрын
Well said
@pericofalcor
@pericofalcor 3 жыл бұрын
@@5h5hz Thanks. At least I was consistent.
@5h5hz
@5h5hz 3 жыл бұрын
@@pericofalcor hahaha such a programmer response, I love it!
@ilyboc
@ilyboc 3 жыл бұрын
He meant powerfull of poo poo
@foxcirc
@foxcirc 3 жыл бұрын
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. 👍
@mCoding
@mCoding 3 жыл бұрын
Do occurrences of metaclass also count towards class?
@photon6156
@photon6156 3 жыл бұрын
Don't worry, if you watch plenty of videos at 2x, you'll get used to it
@foxcirc
@foxcirc 3 жыл бұрын
​@@mCoding No, I counted them seperately, I counted things like "class body" though. Still 70 times seems like a very high number. EDIT: I counted again and updated the numbers, though not much changed.
@georgplaz
@georgplaz 3 жыл бұрын
@@foxcirc was that "edit" a form of youtube commit message? 😁
@mytelevisionisdead
@mytelevisionisdead 2 жыл бұрын
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
@macchicken98
@macchicken98 3 жыл бұрын
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
@pranavnyavanandi9710
@pranavnyavanandi9710 2 жыл бұрын
How do you go about finding what python does under the hood, in a relevant manner?
@ganeshlakshmanan3391
@ganeshlakshmanan3391 2 жыл бұрын
My mind is not blown as these features have been available in other OOP languages for ages. However I still like Python for its simplicity and would dread going into typing library, generics etc. I prefer simplicity.
@yogachen7885
@yogachen7885 3 жыл бұрын
This is the most complicated subject of Python I’ve ever seen. Thank you for your work. #pycharm #clion
@MathMagician93
@MathMagician93 3 жыл бұрын
All programming languages seem to converge towards each other. Combine this with Cython and static typing, and you just made a slower C++, but with more libraries. #pycharm
@chrisvinciguerra4128
@chrisvinciguerra4128 3 жыл бұрын
As the world progresses more we all figure out the best way to do things has been C++ all along.
@duality4y
@duality4y 3 жыл бұрын
@@chrisvinciguerra4128 c++ is not a good choice for embedded systems c ftw
@supernoob0536
@supernoob0536 3 жыл бұрын
@@duality4y May aswell just hammer the opcodes into RAM yourself
@comradepeter87
@comradepeter87 3 жыл бұрын
@@chrisvinciguerra4128 Yeah that beautiful manual memory management when you're designing a high-level user-facing application was always the right way all along.
@mCoding
@mCoding 3 жыл бұрын
Gratz! You (Yannick) get a PyCharm license! Contact me to claim your prize! mcoding.io/contact
@dragon_pi
@dragon_pi 3 жыл бұрын
"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
@georgplaz
@georgplaz 3 жыл бұрын
its not always a good feature
@dragon_pi
@dragon_pi 3 жыл бұрын
@@georgplaz yeah, too much customisation just gets everyone confused, you cant replace every descriptive function name with some __magic__ and have people (and yourself) still understand it.
@jaysonbunnell8097
@jaysonbunnell8097 2 жыл бұрын
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!!
@rafaelhdebarros
@rafaelhdebarros 3 жыл бұрын
This is a terrible idea, super slow, and very very dangerous. I absolutely love it.
@mCoding
@mCoding 3 жыл бұрын
You had me in the first half
@brucewayne1777
@brucewayne1777 3 жыл бұрын
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
@matthieurochette
@matthieurochette 2 жыл бұрын
Same here, my friends call me when they have a question about Python because they know I love it and know it quite well, but when they call me a "Python master" I humble myself by remembering this channel exists :')
@lawrencedoliveiro9104
@lawrencedoliveiro9104 3 жыл бұрын
7:33 This is how the enum module is implemented -- you create enum classes by subclassing base classes that have a special metaclass attached.
@nhoover
@nhoover 3 жыл бұрын
Very clear explanation. After over 10 years of python programming I still learn new tricks from all of your videos. Thanks!
@mCoding
@mCoding 3 жыл бұрын
Great to hear!
@UntakenNick
@UntakenNick 3 жыл бұрын
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.
@dariuszspiewak5624
@dariuszspiewak5624 2 жыл бұрын
David Beazley, as far as I remember, advocates the use of class decorators (where possible) instead of metaclasses. The decorators are easier to handle, easily composable and easier to understand. But sometimes... yes, one has to take the bull by the horns and dive into metaclasses (not for the faint of heart). There's no wizardry at play here but one has to go through a lot of theory and practical examples to really start to see (appreciate?) the structure and intent of the creators of the language. Thank you for the video and spreading the knowledge.
@SeanNicholsEh
@SeanNicholsEh 3 жыл бұрын
Well. That was... mind-bending indeed! Not so much the usage (that was pretty straightforward) but all the details and digressions in the implementation. I always follow along with these videos in my python interpreter, so I can pause the video whenever I'm not certain about something, and play around a bit until I'm sure I've got it, then resume the video. Normally that only adds a few minutes here and there, but this video took me -- I kid you not -- OVER THREE HOURS to get through due to all the times I had to pause it and work out what was going on, not to mention all the scrubbing back and forth to refer to an earlier part of the video, unwinding the code as I went, to understand exactly what was being passed around in a later part. ANYWAY. I think I mostly have it straight now, with one glaring exception: At around 11:23 on line 64, you throw 𝚔𝚎𝚢: 𝙾𝚟𝚎𝚛𝚕𝚘𝚊𝚍(𝚟𝚊𝚕) into the 𝘰𝘷𝘦𝘳𝘭𝘰𝘢𝘥𝘦𝘥_𝘯𝘢𝘮𝘦𝘴𝘱𝘢𝘤𝘦 dictionary. But I don't understand how the instance of Overload gets its __𝘨𝘦𝘵__() method called when it's an element of the dictionary? I would understand if it were an attribute of some class object 𝘢. Which is to say: let's assume we have an instance 𝘰𝘷 of 𝘖𝘷𝘦𝘳𝘭𝘰𝘢𝘥. Then if 𝘰𝘷 were an element of 𝘢, accessing 𝚊.𝚘𝚟 would for sure trigger a call to 𝘢.𝘰𝘷.__𝘨𝘦𝘵__(). But if you have 𝘰𝘷 as an element of a dictionary (let's call it 𝘥) which is in 𝘢, then accessing 𝚊.𝚍['𝚘𝚟'] would presumably only ever trigger a call to 𝘢.𝘥.__𝘨𝘦𝘵__()? So how does 𝘢.𝘥['𝘰𝘷'].__𝘨𝘦𝘵__() ever get called? I'm guessing that it's some magic behind the implementation of 𝘵𝘺𝘱𝘦.__𝘯𝘦𝘸__() ? Do elements of the namespace being passed to that function get transformed into attributes of the class being created? Or am I missing some other intuition here? Other than that, well... I am... enlightened!
@mCoding
@mCoding 3 жыл бұрын
Wow! I'm so happy to hear that you follow my vids to closely, this is surely the best way to learn! Indeed it is some magic behind type's new method. type.__new__(mcs, name, bases, namespace) basically eats the namespace argument and turns a wrapped copy of it into the new class's __dict__. So when I pass the overloaded_namespace in that spot, the overloaded_namespace essentially becomes the class dictionary for the newly created class! So you guessed exactly right!
@SeanNicholsEh
@SeanNicholsEh 3 жыл бұрын
@@mCoding Aha! Awesome. Thanks for the confirmation!
@vanya909
@vanya909 Жыл бұрын
I've faced with problems trying to implement code at 14:30. The key is pylance can't recognize arguments for dynamically generated `__init__()` method and keep warning me. Are there any ways to fix this? I implemented this in order to get autocomplete on __init__ arguments but I'm wondering now if it's even possible.
@ciscoortega9789
@ciscoortega9789 3 жыл бұрын
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!
@secozzi2111
@secozzi2111 3 жыл бұрын
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
@theepicguy6575
@theepicguy6575 3 жыл бұрын
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-
@legotechnic27
@legotechnic27 3 жыл бұрын
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.
@spaghettiking653
@spaghettiking653 3 жыл бұрын
Thanks sir, your content is always enlightening. Frankly, it sometimes stupefies me how much there is to learn about a language that many of my friends, and my computer science teacher, dismiss as simplistic... great content as always! #pycharm
@mCoding
@mCoding 3 жыл бұрын
Glad to hear that! It never ends, I learn many new things every day! Sometimes in the process of recording :)
@comradepeter87
@comradepeter87 3 жыл бұрын
Seriously, if this language had tricks up its sleeves to do a lot of this at compile-time (like Zig), it would've been the most powerful language across the domains.
@comradepeter87
@comradepeter87 3 жыл бұрын
I mean seriously, so easy to pick up and get started, yet so accessible to dive deep and get lost into the wild wild technicalities.
@matthieurochette
@matthieurochette 2 жыл бұрын
@@comradepeter87 You can look into JIT compiling solutions for Python like numba (library) or PyPI (interpreter) for example, it lets the code be compiled at runtime (which slows down the first function call, but generally makes the next ones way faster, catching up to the realm of fully compiled languages)
@CHROMIUMHEROmusic
@CHROMIUMHEROmusic 2 жыл бұрын
@@matthieurochette Yeah numba is awesome!! Relatively simple to use as well
@parlor3115
@parlor3115 3 жыл бұрын
Damn, I've got to appreciate how flexible Python is. This is face meltingly awesome.
@mCoding
@mCoding 3 жыл бұрын
Thanks for the kind words!
@jezreel3584
@jezreel3584 3 жыл бұрын
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
@alexismandelias
@alexismandelias 3 жыл бұрын
Every time I think I am quite knowledgeable in python a new video is posted and I realize how few features of python I actually use
@lxathu
@lxathu 3 жыл бұрын
Now, that we've learnt about this from this fantastic video, probably it's time for the designers of the language to learn, from the very same video, that the most basic feature of am OO language should not be paid in blood.
@mCoding
@mCoding 3 жыл бұрын
Haha you're welcome to suggest but I think the designers are mostly against checking types at runtime. Also they will tell you python is a multiparadigm language, not just OO!
@_gregor
@_gregor 3 жыл бұрын
Awesome video! It's a bit more advanced than usual, but it's good to hear something like that once in a while!
@samuellebot4390
@samuellebot4390 3 жыл бұрын
this is heresies, black wizardry and demonology combined
@Kenionatus
@Kenionatus 3 жыл бұрын
Every use of this forbidden knowledge needs to be marked with # Here be dragons!
@turtlewasnttaken
@turtlewasnttaken 3 жыл бұрын
This was actually quite interesting. I didn't know function overloading was even possible in python! Very cool. #python
@Chris-yw5is
@Chris-yw5is 3 жыл бұрын
For once Python is making me appreciate the simplicity of C++ and not the other way around #clion
@kurtmayer2041
@kurtmayer2041 3 жыл бұрын
don't worry, we'll get reflections in c++ soon(tm)
@duality4y
@duality4y 3 жыл бұрын
@@kurtmayer2041 i need that
@nabeelsherazi8860
@nabeelsherazi8860 3 жыл бұрын
This is so much simpler than anything C++ reflection ever will be. At least this is consistent lol
@tochka832
@tochka832 3 жыл бұрын
simplicity... of C++? in what world is it true lol
@comradepeter87
@comradepeter87 3 жыл бұрын
You must've just started learning C++.
@mirage3dee
@mirage3dee 3 жыл бұрын
Very interesting information there. I would think, though, that most people have gotten around not having overloads built-into standard Python for so long, that normal Python paradigms suffice for all their coding. Overloading is nice to have, but I think it's more of syntactic sugar to other possible syntaxes. Somehow I can't help but think about the Zen of "Explicit is better than Implicit" might apply here as well, especially when usage is coupled with non-keywords args. Thank you for the wonderful videos, and I must admit this is my first comment on your channel, but I follow your videos constantly. #pycharm
@Pyroseza
@Pyroseza 3 жыл бұрын
James, every video I watch you blow my mind with what is possible in Python, I don’t know yet what I’ll do with all this new knowledge but dang is it fascinating! Thanks for the quality content #pycharm
@gawwad4073
@gawwad4073 Жыл бұрын
Your videos are very useful. Short and to the point and ofter cover subjects you may not encounter when using the language even professionally. Thank you!
@sadhlife
@sadhlife 3 жыл бұрын
"a class defn is actually just syntactic sugar for calling a type constructor" There's a tiny caveat here, like being able to pass kwargs to the type definition's bases, which can't be done with `type`. To be able to do that you'd have to use `__build_class__()` instead, so in python3 (i believe starting from py3.1) it's actually sugar for the build class top level function instead. It's nothing major though, but I think if you're making an absolute statement it should be correct.
@pavolkollar7748
@pavolkollar7748 3 жыл бұрын
Damn. The type of video to really smack you in the face, when you think you know programming. Do more, James, your content is fantastic! #pycharm
@jachymmierva9453
@jachymmierva9453 3 жыл бұрын
This is extremely interesting. I tried to learn about metaclasses about two years ago, but I didn't understand it - until now. Excellent explanation.
@mCoding
@mCoding 3 жыл бұрын
Great to hear!
@gideonfelt2819
@gideonfelt2819 2 жыл бұрын
Python gets more intricate and beautiful the more I use it. (and learn from your videos)
@deadmanbg
@deadmanbg 3 жыл бұрын
I'm really happy that you are making speed comparisons. This is something I often do with python, to figure out what works best, but I don't always understand why. #clion
@wizardfix
@wizardfix Жыл бұрын
This is really fascinating stuff James! I thought I was pretty experienced with Python, but you are helping me take my understanding to another level. Many thanks for sharing your wisdom.
@mCoding
@mCoding Жыл бұрын
Hi Wizard, thank you very much for your kind words! I'm glad you enjoyed and glad to have your viewership!
@WilliamDye-willdye
@WilliamDye-willdye 3 жыл бұрын
In every large code base that I've worked in, a few people have tried out obscure-but-extremely-cool language tricks. Most, but not all, of those cases resulted in long discussions with the people who have to maintain the code, test the code, see the code and want to know about it just out of curiosity, or worst of all, were tracking down a difficult bug and wondered if this obscure code they don't understand could somehow be the culprit. If you take into account the cost of those discussions, it's very rare for the trick to provide a clear net benefit. It's happened, but it's very rare. In a large code base, the highest priority is to make the code readable by others. Please remember that before you use a feature that most others do not know.
@mCoding
@mCoding 3 жыл бұрын
I totally agree, I'm often that person tracking down why a framework needed to change everything to save a few characters. But I do love exploring the limits of what is possible.
@mgdibtygd
@mgdibtygd 3 жыл бұрын
Fabulous! Just what I needed to understand this shiny yet murky realm of metaclasses.
@drygordspellweaver8761
@drygordspellweaver8761 2 жыл бұрын
Just some feedback... the constant cuts saves viewing time but also impedes comprehension. It takes you time to talk about these ideas and likewise it takes time to comprehend them.
@gurmindersingh7602
@gurmindersingh7602 23 күн бұрын
Great video! It was very insightful. We definitely need more videos like this with such deep-level insights.
@lawrencedoliveiro9104
@lawrencedoliveiro9104 3 жыл бұрын
4:20 I think the documentation suggests that you can create metaclasses that do not inherit from type. But I haven’t been able to get that to work.
@sadhlife
@sadhlife 3 жыл бұрын
Another little suggestion: you generally shouldn't try to subclass `dict` because it has special cases in the interpreter, you should extend `collections.UserDict` instead.
@NikolajLepka
@NikolajLepka 2 жыл бұрын
a perhaps more efficient way to do overloading is via monomorphisation instead of having one key having a list of possible values that you then need to search through, you instead create a new name for each method based on its argument list and create a method called something like "f_int" and "f_string" and "f_any_any" as the names doing this lets you take advantage of the dictionary's fast lookup without being also limited by the list's slow lookup
@godofnothing520
@godofnothing520 2 жыл бұрын
No, function overloading improves readability and avoids remembering too much function. Instead of having multiple "f_int" and "f_string" and "f_any_any". It is better to only have 1 "f" function to handle all arguments.
@benhbr
@benhbr 3 жыл бұрын
10:48 „owning the dot“ - I consider descriptors rather a way of owning the *equals sign*
@MJAsadi
@MJAsadi 3 жыл бұрын
Imagine forcing the interpreter to do all of that on the fly
@srijanmukherjee4658
@srijanmukherjee4658 3 жыл бұрын
and yet again, I feel like my python programming skill has leveled up. #pycharm Some more C/C++ content would be great.
@DanielLavedoniodeLima_DLL
@DanielLavedoniodeLima_DLL 3 жыл бұрын
I think the good part is that there are so many concepts you touch that I didn't even know in Python. I had to stop several times to look the documentation and learned a lot! Thanks! #pycharm #clion
@djtomoy
@djtomoy Ай бұрын
hold on, slow down a bit.. what do i type again? class?
@bencipherx
@bencipherx 2 жыл бұрын
This is called polymorphism in OOP and also I’ve seen a technique of overloading in Python before, can’t remember for certain now, but I think it’s in one of Corey M Schafer videos
@mattnorth84
@mattnorth84 3 жыл бұрын
Being a beginner with Python I'm quite happy I managed to follow what was going on here. Even though the approach discussed here has various negatives the content as a whole was quite informative. Thank you. #pycharm
@zactron1997
@zactron1997 3 жыл бұрын
I'd love to see a short followup to this concept using the new Python Match syntax, I don't imagine it drastically changing performance, but I could imagine it allowing for a much cleaner code base.
@kurtmayer2041
@kurtmayer2041 3 жыл бұрын
just saying, you can (and i have) implement run-time overloads with just function decorators (which makes it also work on module-level functions)
@mCoding
@mCoding 3 жыл бұрын
Would love to see your implementation!
@oxey_
@oxey_ 3 жыл бұрын
Yet another banger, I didn't even know this existed to be honest! Really interesting watch. As for the giveaway I 'm a student so I get their stuff for free, but it's actually incredible you got that opportunity from Jetbrains! Congrats on that.
@friedkeenan
@friedkeenan 3 жыл бұрын
Yeah, metaclasses are super powerful but also something you should think hard about as to whether they're the right tool for the job. For the purpose of function overloads, I'd probably even say they're not the correct choice, and would just be better just as a property-like decorator where you do @overload for the first definition and then @f.overload for the subsequent definitions. And of course there's the question of whether having overloads in python code is even good
@ErikS-
@ErikS- Жыл бұрын
Excellent video. It took me some time to understand it, but it is pretty genius what you do. I saved this video because of it being so valuable... I see that your example implementation that allows function overloading, indeed causes a big amount of overhead. Maybe its an idea to raise a PEP for getting a real solution that allows us to overload functions in future version of python. Because honestly, I think it is a very worthwile addition to python.
@QuantumHistorian
@QuantumHistorian 3 жыл бұрын
Inheriting from dict at 9:00? I thought standard practice was to inherit from something in collections or abc_collections? Curious as to why @mcoding chose to do it that way.
@mCoding
@mCoding 3 жыл бұрын
Good question! It depends on your purpose. An ABC collection like MutableMapping allows you to take something that is *not* a dict, provide a few methods, and the ABC will fill out the rest of the dict interface. For example, if you were storing data in a list of (key, value) pairs, you could use MutableMapping to make this act like a dict. But in my case I'm still just storing my data in a dict, I'm just intercepting and modifying *what* is being stored. So then what about UserDict from collections? UserDict is used when you do want to store your data in a real dict, so that looks promising, but what is UserDict's real use? The purpose of the UserDict is that it exposes the underlying dict object in its "data" attribute, so at any point if you want to get the underlying representation out you can do that. But all I use this dict for is putting stuff in and then making a normal dict copy of it (namespace always gets copied by type.__new__). I don't modify the getitem of OverloadDict, so the default implementation of making a copy will do the right thing. You could use UserDict in this case, but it's not needed and generally speaking proxy objects always get a performance penalty over subclassing the real thing (not that it matters much since this only happens at class definition time, not instantiation time).
@QuantumHistorian
@QuantumHistorian 3 жыл бұрын
@@mCoding Thanks for the detailed answer. It's amazing that you not only make great videos, but also give highly insightful answers to comments!
@virtualraider
@virtualraider 3 жыл бұрын
@mCoding would a set be a better structure than a list to keep "seen" parameters? In a list you'd run the risk to overload more than once with the same arguments. Unless you want to combine overload and duck typing? 🤯
@sheveksmath702
@sheveksmath702 3 жыл бұрын
Glad you noted the performance hit there; runtime reflection can cause slowness in other languages too (e.g. C#). Things like Rust macros or C# source generators can move this work to compile time (which has it's limits too), but I'm not sure there's any way you could do that in Python.
@janekschleicher9661
@janekschleicher9661 2 жыл бұрын
`eval` and `exec` are there to do such things. (Even though, technically, the compilation will be at runtime for it, but at least this is relatively quick, much faster than introspection, because Python is good and fast at parsing and compiling Python). If you need it quicker (bypassing the needed compilation at runtime), you can also generate/template the compiled bytecode, that's possible, too and if the templating is fast (it should be usually), then you have close to no overhead. But of course, you need to be relatively good in understand Python Bytecode and working with the disassembler to get there. (Tbh, I only know theoretical that it works from other author's articles, but never implemented it on my own). And of course, you still can take the same approach as C/C++ and write a code replacement and generation engine with all the same caveats as in C/C++. In some areas, this is even regarded as a good practice, e.g. when working with protobuf. The biggest advantage is, that it allows to have an easy IDE integration, as the IDE can inspect statically what's going on. Otherwise, the IDE needs internal support (that's what's happen in PyCharm for Django as an example where also a lot of code is generated dynamically - there also mainly for performance reasons over any introspection/reflection injection)
@evilyach
@evilyach 3 жыл бұрын
Drinking game: you take a shot every time James says "class"
@HeyImAK
@HeyImAK 3 жыл бұрын
I feel like this channel both teaches you advance python and the foundations you didn't know you don't know. Thanks! #pycharn
@herogpi1
@herogpi1 3 жыл бұрын
Never thought we could define two functions with same name but receive differents parameters, like I was used in C++. That's very nice! #pycharm
@charly7842
@charly7842 2 жыл бұрын
Is it old-fashioned to implement the multimethods module to invoke function overloading in Python? Which is the more convenient method of overloading functions in Python?
@GuyMichaely
@GuyMichaely 2 жыл бұрын
6:58 "__call__ is called whenever you try to make an instance of the class" Isn't __call__ usually called when an instance of the class is called, not the class itself? Is it different for classes that inherit from type?
@mCoding
@mCoding 2 жыл бұрын
Dunder call is called when you put parens after the object. If your object is a type, "calling" the class object semantically means making an instance of that type.
@Songfugel
@Songfugel 2 жыл бұрын
Seems like the "new" structural pattern matching would be the perfect match (pun unintended) to implement python function overloading
@philippedias4357
@philippedias4357 3 жыл бұрын
At least a clear and also deep explanation of metaclass mechanism and some uses. Not sure I will ever use it since changing much class behaviour makes code difficult to maintain. Thanks man. #pycharm which is my favorite IDE for python.
@tytywuu
@tytywuu 3 жыл бұрын
learning these bits of lower-level mechanisms in Python lets me understand how Python magic works, especially for those built-in decorators like property. #pycharm #clion.
@AJMansfield1
@AJMansfield1 3 жыл бұрын
Oh wow I didn't know you could do actual-for-real code generation with python metaclasses. I do a lot of multi-language development, and that often means needing to use a common configuration file between all the different languages and platforms to perform language-specific code generation, just to make sure that all of the implementations stay in sync and we don't introduce bugs where one language treats some byte in a packet different from another language. If I can just read the config and do the code generation at _import time_ in python, that'll cut out the need to add a whole separate build step for every change and make iterating so much faster.
@gregh6586
@gregh6586 3 жыл бұрын
James Powell had a great talk at some PyData which included metaclasses though your introduction is much more practical. Nice one! #pycharm
@LittleCrazy2life
@LittleCrazy2life 3 жыл бұрын
7:52: "No need to be confused" make me to watch multiple times to grasp. #pycharm
@aaronm6675
@aaronm6675 3 жыл бұрын
Great video, i had no idea these aspects where so customizable!
@thatinstant
@thatinstant 3 жыл бұрын
Even after watching this video, metaclasses still seem magical. There's no end to what you can do with them. #pycharm
@liesdamnlies3372
@liesdamnlies3372 3 жыл бұрын
Okay, okay. I’ll bite: #pycharm #clion Thanks for another great video. My brain did a double-take at the start, I was questioning myself “wait no, Python doesn’t have function overloading…” mCoding: “Python doesn’t have function overloading.” Me: “oh thank god” mCoding: “Except when it does.”
@TheGreatAkhillis
@TheGreatAkhillis 3 жыл бұрын
Well that's the first video that I watch on your channel that left me puzzled yet excited to watch it again #pycharm
@mCoding
@mCoding 3 жыл бұрын
I'm sure you are in good company, metaclasses are probably the most difficult thing to master in Python. Master metaclasses and you can master the whole language!
@tsunekakou1275
@tsunekakou1275 3 жыл бұрын
C++ will get metaclasses (hopefully C++2b). It's static code generation use static reflection, and generative metaprogramming. #clion
@mCoding
@mCoding 3 жыл бұрын
I know I can't wait to try it!
@tsunekakou1275
@tsunekakou1275 3 жыл бұрын
If you don't mind the hassle you could try lock3 implementation, given it's still in experiment, very buggy (can't iterate over template parameters, kinda stuck there) and crash a lot... but it is amazing. I wish i could have this feature right now (or at least stable enough), 3 years is a long wait.
@PythonisLove
@PythonisLove 3 жыл бұрын
now everytime someone says, learning python is easy, I will forward this video 🙂
@chandramgc
@chandramgc 3 жыл бұрын
WoW !!! Metaclass are overwhelming. But they are quite amazing. And really like your efforts in teaching these concepts to us. This is the most interesting KZbin channel out of all my subscribers #pycharm
@jakubzurakowski8815
@jakubzurakowski8815 3 жыл бұрын
I probably will never use Metaclasses personally, but it's great to know that they're there and what they do! #pycharm #clion
@SolidBuildersInc
@SolidBuildersInc 3 жыл бұрын
#Python Question This is getting very close to what I am attempting to do using a Groupby with a Lambda Series for multiple function calls and appending the results to a growing List if it rings True from the Function. Would you suggest this is doing essentially the same thing ? Do you have a deep dive on Groupby with Custom Lambda Function with a Series ? Thanks for any references that supports this effort.....
@voxelfusion9894
@voxelfusion9894 3 жыл бұрын
The question is, are these fancy overloads worth the hit at runtime? In C++ all of that overhead happens at compile time, but here it isn't so. #pycharm
@pranavnyavanandi9710
@pranavnyavanandi9710 2 жыл бұрын
I have trouble understanding descriptors and metaclasses - perhaps the two constructs underlying Python's various features.
@POINTS2
@POINTS2 3 жыл бұрын
Is method overloading something that should be done in practice in Python? It seems like this is a way to do it but because it can't be done directly, I wonder if the intention was to not allow for overloading methods.
@mCoding
@mCoding 3 жыл бұрын
Definitely not something that should be done in practice. It essentially involves decision-making based off the type of an object, which goes against Python's duck typing philosophy.
@Prizeadvisor
@Prizeadvisor 8 ай бұрын
how can i understand pthon the way you do?
@tHe0nLyNeXuS
@tHe0nLyNeXuS 3 жыл бұрын
Really neat! Just a question: why not use a dict of signature hashes resolve the best_match? I feel like it would be faster than iterating over the list of signatures.
@calvindang7291
@calvindang7291 3 жыл бұрын
This stuff is super cool. That function overloading still looks like black magic after you explained it. #pycharm
@realabja1
@realabja1 3 жыл бұрын
But what about singledispatch overload?
@andrewhicks
@andrewhicks 3 жыл бұрын
Amazing. I’ll definitely be needing to watch this vid again a few times to understand it, but it’s truly mind blowing what you can do with metaclasses. #pycharm
@unflexian
@unflexian 3 жыл бұрын
Wonder if any of this could be moved from the runtime to before, maybe with cpython #pycharm #clion
@makhdv
@makhdv 3 жыл бұрын
it seems meta could allow avoid using explicit self. which makes a little pain boilerplate code. Via adding self on __prepare__.
@elianangius6465
@elianangius6465 3 жыл бұрын
Awesome under-the-hood demo implementation of Python’s missing overload OO functionality. #pycharm
@FranziskoGomes
@FranziskoGomes 3 жыл бұрын
Thank you for your dedicated work, James Murphy. This video proves it once again. #pycharm
@mCoding
@mCoding 3 жыл бұрын
My pleasure!
@jameswestwood1268
@jameswestwood1268 2 жыл бұрын
I was quite lost in this video. What are the pre-requisites to understanding it? Can you point me to other videos which would build my knowledge up to understanding the concepts in the video? Thanks.
@l4luchi123
@l4luchi123 3 жыл бұрын
Loved the part where you showed what's behind the curtain of some standard libraries. You should make a whole series of this. 👏🏻 #pycharm
@sab611
@sab611 3 жыл бұрын
#pycharm wouldn't this be much more efficient in a functional language using pattern matching? seems too messy and complex in Python.
@marckiezeender
@marckiezeender 2 жыл бұрын
sounds like once you could also put a __call__ method in the OverloadList, which just calls the proper element in itself
@cemsity
@cemsity 3 жыл бұрын
Metaprogramming and Reflection might be a little over my head, but this is way easier to read, than other methods for python overloading even if it does cost some time. #pycharm
Python Generators
15:32
mCoding
Рет қаралды 144 М.
super/MRO, Python's most misunderstood feature.
21:07
mCoding
Рет қаралды 222 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
Правильный подход к детям
00:18
Beatrise
Рет қаралды 11 МЛН
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 822 М.
Every Python dev falls for this (name mangling)
14:11
mCoding
Рет қаралды 140 М.
Avoid These BAD Practices in Python OOP
24:42
ArjanCodes
Рет қаралды 82 М.
Expert Python Tutorial #3 - Metaclasses & How Classes Really Work
20:41
Please Master This MAGIC Python Feature... 🪄
25:10
Tech With Tim
Рет қаралды 113 М.
__new__ vs __init__ in Python
10:50
mCoding
Рет қаралды 210 М.
Python itertools - The key to mastering iteration
20:03
mCoding
Рет қаралды 36 М.
8 things in Python you didn't realize are descriptors
14:21
Metaclasses in 17 minutes
17:29
Carberra
Рет қаралды 5 М.
Protocol Or ABC In Python - When to Use Which One?
23:45
ArjanCodes
Рет қаралды 206 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН