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 trialAnthony Grodowski
4,902 PointsI'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:
Why are we using
self = str.__new__(*args, **kwargs)
inclass ReversedStr(str)
? It looks like it's simillar to usage ofsuper()
, but I don't get from which class we're inheriting that.What's going on with
super().__init__()
inclass 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?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 justcopy
before and I don't get why another one is neccessary.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 exampleclass FilledList(list):
?And finally, the last part of the video with JavaScriptObject is unclear to me, especially
self[item]
andsuper().__getattribute__(item)
- from what are we inheriting now by using super?
Please help me with this big confusion!
3 Answers
Anthony Grodowski
4,902 PointsFor 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
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points1) 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
4,902 PointsThanks Henrik!
But in what way are we customizing it right here?
And what's the purpose of doing it? What does it mean that it's a constructor of the super class?
I meant what is the purpose of putting for example
list
inclass FilledList(list)
So what
dict
does in here?
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points1) 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
Brandon Thompson
3,018 PointsGuys should really consider re-doing this section. Kinda all over the place with the explanation.
Thomas Bynum
4,616 PointsThomas Bynum
4,616 PointsThat video made so much more since than the lesson from Kenneth. I feel like I have a good grasp of subclassing now.
James Arnold
3,986 PointsJames Arnold
3,986 PointsThank you so much! This makes it very clear.