Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Advanced Objects Math

self.value and self?

How come we dont use self.value + other ? Why we use self + other instead ?

4 Answers

Steven Parker
Steven Parker
210,424 Points

By 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.

Steven Parker
Steven Parker
210,424 Points

These 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
Chris Freeman
Treehouse Moderator 67,636 Points

Also consider that in the expression self.value + other, "self.other" would be a string and would then trigger the str.__add__ method which would result in string concatenation instead of the desired numerical addition.

I still don't understand why he's using self instead of self.value? Could you please explain it with examples?

Steven Parker
Steven Parker
210,424 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
Jamaru Rodgers
3,046 Points

Hi 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:

  1. Why not just use "self" instead of "self.value" for everything?
  2. 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
Steven Parker
210,424 Points

Internally, "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
Jamaru Rodgers
3,046 Points

Steven Parker, Roger that! Thanks, your input is always Awesome!