super/MRO, Python's most misunderstood feature.

preview_player
Показать описание
Python's super does NOT mean "parent".

It means "next in line". What line? The Method Resolution Order (MRO) of an object, which defines the search order for attribute lookups. super() uses some very sneaky techniques, such as examining the current stack frame, and object proxying. All this is explained in great detail, for both single and multiple inheritance, and a pure Python implementation of super is given.

CONTEST CURRENTLY CLOSED! THE WINNERS HAVE BEEN CONTACTED, CHECK YOUR COMMENT TO SEE!

OFFICIAL CONTEST RULES:
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 somewhere in the comment.
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.
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.

SUPPORT ME ⭐
---------------------------------------------------

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

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------

CHAPTERS
---------------------------------------------------
0:00 Intro
0:32 Basic super usage
2:34 super does NOT call your parent
4:29 Method Resolution Order (MRO)
6:18 super means "next in line"
6:48 How MRO is determined
8:07 How to design with super
12:01 How does Python super work?
16:53 Two-argument super
18:01 One-argument super
18:18 Implementing super in Python
Рекомендации по теме
Комментарии
Автор

Isn't the search that the MRO is doing the breadth-first search?

MrBughyman
Автор

i spent days treading in convoluted documentation to finally understand MRO, multiple inheritance, and super (had to also get through descriptors to understand the bounding mechanics)... and here it is, a 20 minute video that explains everything. this channel is way too underrated.

nou
Автор

I've been programming in Python professionally for almost 10 years and I learned something in this video.

AtriumComplex
Автор

This is why multiple inheritance, especially "diamond" multiple inheritance is evil. Even if you're one of the 0.01% who knows how it works, it will still trip you up when you dont expect it (that is, 99% of the time). Having recently come across C#'s solution to this (a class can only inherit from 1 class, but can inherit from as many Interfaces as it wants), I now think python's approach is too liberal. Of course that's the whole python ethos, but sometimes it creates far more traps than necessary. #pycharm

QuantumHistorian
Автор

The *level* of this channel is 👌. I've been coding in Python for quite awhile and I always learn stuff here. It's really refreshing when a lot of what is out there is like "how to use dicts in Python!"

Mutual_Information
Автор

The only time I used multiple inheritance it was a big mistake, it became impossible to have the classes cooperate nicely. A slightly more verbose solution based on composition solved the same problem without all the subtle bugs

#pycharm

Buoy
Автор

Thanks for being so clean and straight-to-the-point in how you present your topics. Your videos are quite like 3Blue1Brown but for programming. Keep up the dry humor, stellar stuff. #pycharm

biffenb
Автор

Amazing insights into the super() function implementation in Python! Thank you for your great work, I always learn something new!

lepsycho
Автор

James: your expert knowledge of not just Python, but C, and CPython implementation - along with dense and info-rich presentation - make for a quite rare resource of deep, under-the-hood understanding of Python. Thanks for putting in the work!

JamesxBond
Автор

The 2 argument super used to be the required way of calling super in python2. So glad we don't need that anymore

JohnHollowell
Автор

Even though I am aware about using super and cooperative multiple inheritance when I watched the iconic talk of Mr. Raymond Hettinger on super()...

But even then, my mind was simply in awe when you dived into the details... I never even imagined that super class is sort of implementing a Proxy pattern...

MsSuyash
Автор

13:18 little mistake. __getattr__ is called whenever getting attribute is failed, __getattribute__ is called everytime you trying to get an attribute.

ТимурТимергалин-вь
Автор

IIRC, you can rebind super to have the compiler magic keep working while also allowing you to change its behaviour (it's definitely the case that using super bound to another name causes the compiler magic to fail)

EDIT: To be clear, if you had written `class super:` instead of `class Super:` it would be a drop-in replacement

Starwort
Автор

Very nice! Your example code at the end is the part that was missing in PEP 3135 back when it was written, it would have cleared up how super works for most people. For a good example, look at PEP 343 - with Statement, where the "Specification" section shows exactly how it works in a very clear fashion.

ddEEE
Автор

A good addition for cooperative inheritance is that the root class' sink method verifies that both *args and **kwargs are empty at that point and throws an error if either isn't. This helps prevent unnecessary arguments being passed somewhere in the chain that stick around after changes/refactoring as the *args and **kwargs in the signatures of children mean that static code analysis often won't see anything wrong.

sirmidor
Автор

Another great video, the first half was satisfyingly confirming some practices I have picked up by using python for a while, and the latter half was part mind-boggling, part "oh please don't do that". Learned a lot as always, thank you James! #pycharm

UnFallenRain
Автор

Fantastic introduction to super() and MRO, thanks.

unpythonic
Автор

And here I thought “How complicated can super() really be?” It’s a joy to learn from you. Keep up the great content! #pycharm

devonlee
Автор

Intermediate programming channel are so rare.
It's always a pleasure watching your videos !
#clion

e
Автор

I remember that I watched Raymond Hettinger's "Super considered super!", understood a little more about the MRO but could not understand how `super()` works.
Now I have watched this video and I have learnt something new today! Thank you!

I still don't fully understand the `super(...)` with argument(s) form, but I guess/hope I don't need it since I don't need to consider my job security. 😂

NicolasChanCSY