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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![QUYEN NGUYEN](https://secure.gravatar.com/avatar/f10ff9221c1030aec9013e2bfda8a245?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
QUYEN 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](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 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](https://secure.gravatar.com/avatar/a3e3feba347cdd7ac8c793be3475a802?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
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](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 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](https://uploads.teamtreehouse.com/production/profile-photos/3111642/micro_00100sPORTRAIT_00100_BURST20180602115345884_COVER.jpg)
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](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 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](https://uploads.teamtreehouse.com/production/profile-photos/3111642/micro_00100sPORTRAIT_00100_BURST20180602115345884_COVER.jpg)
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
230,688 PointsSteven Parker
230,688 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,425 PointsChris Freeman
Treehouse Moderator 68,425 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.