Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Advanced Objects Subclassing Built-ins

what does the super()__getattribute__(item) here mean?

I'd like to ask a question about the super() function used here.

From previous courses I see that super() is used as a subclass to inherent the functions of its superclass. Here I'm kind of confused about the code this line: super()__getattribute__(item) What I thought is that super() here let the __getattribute__ method jump out of the function to the class JavaScirptObject level, so is that a new situation that super() can be used rather than inheritance of classes? Look forward to any further explanation about this.

[MOD: added ` formatting -cf]

mourad marzouk
mourad marzouk
5,560 Points

Hi Chen,

I think in this case he is using super() as a fail safe. So if an attribute was to be added that isn't there the try/except would catch it then push it back to the over arching class above. It will then look are an attribute with that name instead of a key. Hope that helps or makes sense, I'm still wrapping my head around it too, but that's what I understood from it, hope I'm right.

2 Answers

Wow, this should be a must-read for everyone who is watching this video.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Good Question. Let's review exactly what super() is providing in the JavaScriptObject.__getattribute__() method.

First, as Kenneth said, the __getattribute__() method is called whenever a attribute is referenced using the "dot notation", as in, object.attribute.

When __getattribute__() is called on a JavaScriptObject instance, Python first looks to see if JavaScriptObject has defined this method, if not it would continue the search through the Method Resolution Order (MRO) defined by the declared parent classes. In this case, dict is the only parent. Since a __getattribute__ method has been defined, the method dict.__getattribute__() would be completely ignored, that is "overridden".

In the JavaScriptObject, the objective is to change the default behavior of the dot notation to instead look up the "attribute" in the dictionary object using the attribute as the dictionary key. However, there my be actual object attributes inherent to the instance that are not part of the dict data. To allow access to these "actual attributes", a super() call is made to access the unaltered version of __getattribute__ that would have been inherited from the parent dict had a local method not overridden it.

To be clearer, while super() allows access to the parent class's version of __getattribute__, it is executed in the context of the JavaScriptObject instance. Meaning any attributes retrieved would, first, be looked for on the current instance, then, if still not found it would look to the JavaScriptObject class attributes, some of which may have been inherited from the dict class attributes` if they exist.

Why go through this extra step of including super()? Because the inherited version of __getattribute__ also contains the code to properly raise errors if the requested attribute is not found.

Please post back if you need more help on this. Good luck!!