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

Anthony Grodowski
Anthony Grodowski
4,902 Points

I've never been more confused after watching a single video

Okay this video is full of things that need for me a better explenation than Kenneth has provided in his video. I'd be very grateful to someone who can can accept that challange(:

Starting off:

  1. Why are we using self = str.__new__(*args, **kwargs) in class ReversedStr(str)? It looks like it's simillar to usage of super(), but I don't get from which class we're inheriting that.

  2. What's going on with super().__init__() in class FilledList(list)? Kenneth says something like the purpose of doing that is ignoring whatever comes in with *args and **kwargs. If that's the point, why can't we just delete them?

  3. Whole explenation of usage of copy.copy(value) is messed up and Kenneth goes back and forth explaining it... Can someone clarify this for me? I've seen just copy before and I don't get why another one is neccessary.

  4. What exactly is the purpose of ?extending(don't know how to call it)? a class with an object like str like it happens in for example class FilledList(list):?

  5. And finally, the last part of the video with JavaScriptObject is unclear to me, especially self[item] and super().__getattribute__(item) - from what are we inheriting now by using super?

Please help me with this big confusion!

3 Answers

Anthony Grodowski
Anthony Grodowski
4,902 Points

For everybody that has the same problem with undertanding the usage of subclassing built-ins, which Kenneth explained poorly in his video: https://www.youtube.com/watch?v=Pz403e3IaZo

Thomas Bynum
Thomas Bynum
4,616 Points

That video made so much more since than the lesson from Kenneth. I feel like I have a good grasp of subclassing now.

James Arnold
James Arnold
3,986 Points

Thank you so much! This makes it very clear.

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

1) When customizing a mutable object (i.e. a list) you'd use __init__ and when customizing an immutable object like a string you'd use __new__

2) super().__init__() calls the constructor of the super class (list) before running the rest of the code in the child class (FilledList)

3) About copy I'd recommend try to read the about shallow copy (copy by reference) and deep copy (copy by value) https://docs.python.org/3/library/copy.html

5) The super() here refers to dict

class ChildClass(SuperClass): super always refers to the SuperClass

I'm not sure what exactly it is you're asking in question 4

Anthony Grodowski
Anthony Grodowski
4,902 Points

Thanks Henrik!

  1. But in what way are we customizing it right here?

  2. And what's the purpose of doing it? What does it mean that it's a constructor of the super class?

  3. I meant what is the purpose of putting for example list in class FilledList(list)

  4. So what dict does in here?

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

1) We're reversing the string: hello would be olleh

2) A constructor constructs an object and the super class is the class that is being inherited from

3) By putting list in class FilledList(list) we can use whatever functionallity is implemented in the list class. This way we don't need to write that much code ourself.

4) See 3 - We can use functionallity implemented in dict

Guys should really consider re-doing this section. Kinda all over the place with the explanation.