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 trialNoor Elharty
2,630 Pointsi didn't understand the last part of the video (subclassing dict)
i lost it at the end of this video (subclassing dict part) i didn't get what he was aiming for , what is the whole point of this JavaScriptObject class ? , why he is subclassing dict ? why using getatrribute method ? why using try except here ? if some one can break down a little for me to understand i would be more than thankful
3 Answers
Dave StSomeWhere
19,870 PointsOK Noor, here's what I get from this section of the vid...
i lost it at the end of this video (subclassing dict part) i didn't get what he was aiming for
what is the whole point of this JavaScriptObject class ?
The goal is to use javascript dot "." notation to access a dictionary entry - look up keys with a dot
dict = {'name':'dave'} # we have a dictionary
dict['name'] # get the name attribute of the dictionary - returns 'dave'
dict.name # try to get the name attribute of the dictionary - returns AttributeError...
why he is subclassing dict ?
Since we like everything about the dict class and just want to add the ability to use dot notation - let start with the dict class as our base class
why using getatrribute method ?
That's the magic method used for getting the attribute when using dot notation (he mentions that a little after the 8 minute mark)
Thus by overriding this magic method we can now use the dot notation
why using try except here ?
To handle the condition when the attribute doesn't exist to allow the original class functionality raise the exception (being Pythonic and DRY)
Does that help at all?
Ayman Said
Python Web Development Techdegree Student 14,717 PointsHi Dave StSomeWhere & Noor Elharty,
My unclear part is how it was used for kind of "set attribute":
jso.language = 'Python'
I got the point of overriding the magic method:
__getattribute__
to have a meaning for the dot. but as far as understood (from the video) it was used to get attribute and not to set! Can you please clarify?
Thanks in advance.
Dave StSomeWhere
19,870 PointsHi Ayman Said,
I'm not sure what you are asking - could you maybe provide a little more context?
Ayman Said
Python Web Development Techdegree Student 14,717 PointsDear Dave StSomeWhere , The dot notation was used to get the value of jso dictionary of the 'language' key and that was clear. My question was regarding on the way this key-value pair was added to the dictionary. as we use the dict[key] = something to add a key-value pair and in the code (video @10:28) I noticed the dot notation was also used! After further thinking I get it, as the magic method:
__getattribute__
Will also be called while setting a new key-value pair.
Thanks man, it clear for me now.
Noor Elharty
2,630 PointsNoor Elharty
2,630 PointsThanks Dave! it's much clearer now.