简体   繁体   中英

Please explain what really happens in description

I was reading the Python crash course to start learning python and when there came a part

>>> favorite_language = 'python '
>>> favorite_language
'python '
>>> favorite_language.rstrip()
'python'
>>> favorite_language
'python '

The author then mentioned that the use of method rstrip() is temporary and to make it permanent we have to do following

>>> favorite_language = 'python '
>>> favorite_language = favorite_language.rstrip()
>>> favorite_language
'python

I am confused in second step here

 >>> favorite_language = favorite_language.rstrip()

if we see from basics when python is interpreting this and reaches second step it thinks that it has to store a new value for favorite_language and it formats the variable and starts storing new value but after = a method is called on the same variable to store value for the same variable.

Shouldn't this cause error?

The favorite_language = favorite_language.rstrip() reassigns the favorite_language with new value, which is the value returned from the favorite_language.rstrip() .

The favorite_language.rstrip() itself does not change the original string ( str() in Python is immutable in any case) just by calling the method. To have the favorite_language modified we have to explicitly say:

"Hey, favorite_language , here's new value for you" .

Which is: favorite_language = new_value

But in this case the new value is the value that the favorite_language.rstrip() returns.

All the expressions on the right side of the = are evaluated before any assignment happens . Also, since str() in Python is immutable, calling favorite_language.rstrip() is not modifying anything, it produces new value , which then can be stored in the favorite_language . So, the production of the new value happens before any reassignment.

For more on the evaluation order ( and many more ), check the documentation .

In basically every imperative language (and many others,), assignments look something like

lvalue ← expression

where the semantics are "evaluate expression, and assign the result to lvalue".

There are many situations where it would be very cumbersome indeed if you could not use lvalue itself in expression, and have it refer to the previous value of lvalue . There are many algorithms where you need to add or subtract one from an index variable, or add a new term to an accumulator. Conversely, there are no situations where it's a useful feature to not be able to do so. But some primitive languages really did require you to use a temporary variable in these scenarios.

The fact that the assignment operator in Python (and C, and a lot of other languages) looks identical to the mathematical symbol for equality is confusing to some beginners; this is why some languages use a left arrow instead, or a stylized ASCII approximation like := (in Pascal; and now also with slightly different semantics in Python) or :- (in eg Prolog).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM