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

Bubbly Sprout
Bubbly Sprout
2,472 Points

How to interpret self.append(...)?

In the video, the teacher has a line of code: self.append(...) My understanding is that self refers to an instance of the class, in this case Hand. Since Hand is inherited from List, Hand should have an attribute that is a List. The above line of code seems to append a new element to that List. But I'm wondering why the name of that List, say aList, is not used here, i.e., shouldn't it be self.aList.append(...)? The reason I'm asking is that in Hand class, one can include other attributes, including another list. If we don't include the list name, how does the computer know which attribute list to append to? Many thanks!

Can you provide a link to the video?

1 Answer

Michael Hulet
Michael Hulet
47,912 Points

Hand inherits from List, which means that Hand is itself also a List. That means that all the same functionality that List has, Hand has also. Thus, it's perfectly valid to call append on an instance of Hand, because you can also do that on an instance of a List

Bubbly Sprout
Bubbly Sprout
2,472 Points

That makes sense! Thank you for the reply! I guess the computer first looks for the append() method in Hand definition. When it can't find it, it looks in the parent (in this case List) definition. When it uses the append() method defined in List, the attribute of List is used.