Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a = b = c
#1
applying the TIASIIW method i found that
a = b = c
assigns c to both a and b.
it works in both 2.7.12 and 3.5.2.
i cannot find this in the reference manual.
is this legal/official coding?  did i misunderstand the reference manual?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
In C, you could do the same (so long as all variables had been declared
and were of the same type.

In python, you should be aware of type changes, example
Note the second example where c changes type, a nd b remain same value, same type as previous.
c = 5
a = b = c
print(a, b, c)
c = 2
print(a, b, c)
print ('type(a): {}, type(b): {}, type(c): {}'.format(type(a), type(b), type(c)))

c = 14.5
print(a, b, c)
print ('type(a): {}, type(b): {}, type(c): {}'.format(type(a), type(b), type(c)))

a = b = c
print(a, b, c)
print ('type(a): {}, type(b): {}, type(c): {}'.format(type(a), type(b), type(c)))
results:
Output:
5 5 5 5 5 2 type(a): <class 'int'>, type(b): <class 'int'>, type(c): <class 'int'> 5 5 14.5 type(a): <class 'int'>, type(b): <class 'int'>, type(c): <class 'float'> 14.5 14.5 14.5 type(a): <class 'float'>, type(b): <class 'float'>, type(c): <class 'float'>
I always used it in C, but try not to not in python, it is legal, and called multiple assignment
It's in the docs, I know I've seen it before, but I can't find it
Reply
#3
Documentation » The Python Language Reference » 7.2. Assignment statements
Python docs Wrote:assignment_stmt ::=  (target_list "=")+ (starred_expression | yield_expression)
An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right.
Reply
#4
(Oct-28-2016, 07:33 AM)Skaperen Wrote: applying the TIASIIW method i found that
a = b = c
assigns c to both a and b.
it works in both 2.7.12 and 3.5.2.
i cannot find this in the reference manual.
is this legal/official coding?  did i misunderstand the reference manual?

Yes it works, legally, officially. What does TIASIIW mean though?
Reply
#5
TIASIIW = test it and see if it works

i know it works in C.  i do it in C a lot.  it works in C because in C an assignment is an expression that yields the value being assigned.  this does not seem to be the case in Python and i am not using any commas.

or TIASIIW = try it and see if it works
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
YSNUAOTFEWIRLPISIITR


Quote:You should not use acronyms on the forum, especially when its really long, plus it says it in the rules
http://python-forum.io/misc.php?action=help&hid=21
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020