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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -