osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Plumbing behind super()


On 28/06/19 12:13 PM, adam.preble at gmail.com wrote:
> I'm trying to mimick Python 3.6 as a .NET science project and have started to get into subclassing. The super() not-a-keyword-honestly-guys has tripped me up. I have to admit that I've professionally been doing a ton Python 2.7, so I'm not good on my Python 3.6 trivia yet. I think I have the general gist of this, but want some affirmation.
> 
> If you use super() in a method, all it does is load super as a global on to the interpreter stack and call it without any arguments. So I'm left to wonder how it's able to figure anything out when it's being literally given nothing...
...

> I was thinking maybe self has become more special in Python 3.6, but I don't think that's true since I've ported code to Python3 before that had inner classes where I'd use "inner_self" to disambiguate with the outer self. And though I thought it was so at first, it just turned out I screwed up my little code snippet to expose it. If self was special then I presume I could find it in my lookups and inject it.
> 
> So how do I go from CALL_FUNCTION on a super() global without anything else on the stack to somehow having all the information I need? Is there something tracking that I'm in an object scope when calling stuff?


You are missing quite a bit by not becoming familiar with Python2 -> 3 
differences!

I'm mystified by "literally given nothing".

1 All 'new' classes descend from object.
2 class Son( Father ):... literally gives 'Father'

(At present) The base class must have been defined (frame on the stack) 
before it may be referenced.

Self does not require super, it refers to the class itself.

If a class has not defined an attribute, eg self.my_attribute, but the 
base class has defined an attribute of that name, then self.my_attribute 
will as initialised by the base class. Welcome to the intricacies of 
managing scope!

-- 
Regards =dn