Python Plus-Equals Syntax Error for Items in List -
why python code result in syntax error @ equal sign? can it?
start_pos = [0, 0] direction = [1, 1] end_pos = [start_pos[0] += direction[0], start_pos[1] += direction[1]]
i had expected instantiate end_pos
, increment start_pos
direction
, in ruby.
as @pm 2ring said, assignments don't return value in python.
lists mutable. can define addition assignment returns result.
for instance:
>>> def add_assignment(a, b): ... a[0] += b[0] ... a[1] += b[1] ... return ... >>> start_pos = [0, 0] >>> direction = [1, 1] >>> end_pos = add_assignment(start_pos, direction) >>> print start_pos, end_pos [1, 1] [1, 1]
Comments
Post a Comment