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

Why doesn't Python print attributes of the JSO that were added through __getattribute__?

Hello. I am following along with Kenneth but am not fully understanding:

  1. Why, after adding the attributes 'fake' and 'language'to jso, the function

    print(fso)
    

    only returns the name attribute that was added when it was created:

    jso = JavaScriptObject({"name":"Kenneth"})
    

    Perhaps I've made an error in my code but I receive no errors when executing

  2. Also, I don't fully understand the purpose of:

        except KeyError:
              return super(JavaScriptObject, self).__getattribute__(item)
    

why would the results of getattribute(item) from the dictionary class produce different results?

Any and all insight would be greatly appreciated!

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points
  1. Why does the print only return the name?

In a print context, the return value of fso (jso?) would be the results of the __str__ method. Perhaps this method only returns the JSO name attribute.

  1. What is the purpose of the code:
    except KeyError:
          return super(JavaScriptObject, self).__getattribute__(item)

When getting a local instance attribute, a KeyError may be raised if the attribute is not found. In this situation, instead of raising the error, the code calls the parent class's __getattribute__ to access the class attributes.