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 trialQUYEN NGUYEN
5,100 Pointsself.value and self?
How come we dont use self.value + other ? Why we use self + other instead ?
4 Answers
Steven Parker
231,248 PointsBy using "self", the appropriate conversion method (either "__int__
" or "__float_
") will be invoked automatically to return the value in the correct format for what it is being combined with.
Kafe Hezam
11,070 PointsI still don't understand why he's using self instead of self.value? Could you please explain it with examples?
Steven Parker
231,248 Points"self.value" is always a string. But using "self" implicitly invokes the correct conversion to match what you are doing with it.
Jamaru Rodgers
3,046 PointsHi Steven Parker,
So you're saying that when using "self.value", we're always going to be a string because we converted it to one with str right? And when we use "self" by itself, "self" automatically conforms to the data type we need it to be? There's no need to convert the type with it at all?
If this is the case:
- Why not just use "self" instead of "self.value" for everything?
- Couldn't we just keep converting "self.value" to the data type we needed in each situation?
I know using self by itself if probably more efficient as far as the py system goes, but wouldn't they both work? I was also confused as to why he used both ways all in the same class.
Thanks!
Jamal Rodgers
Steven Parker
231,248 PointsInternally, "self.value" is used to get the stored string and do things with it directly. It's important to do this inside the magic methods that perform the conversions to not create a recursion loop. In other methods, it's OK to use just "self".
Jamaru Rodgers
3,046 PointsSteven Parker, Roger that! Thanks, your input is always Awesome!
Ben Hedgepeth
Python Web Development Techdegree Student 10,287 PointsBen Hedgepeth
Python Web Development Techdegree Student 10,287 PointsIs there some documentation or a reference that you can link that talks about this?
Steven Parker
231,248 PointsSteven Parker
231,248 PointsThese are in the Python documents under Emulating numeric types, but you can find a number of tutorials with a web search. Try looking for "Python magic methods".
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsAlso consider that in the expression
self.value + other
, "self.other" would be a string and would then trigger thestr.__add__
method which would result in string concatenation instead of the desired numerical addition.