Python variable assignment order of operations
Is there a way to do a variable assignment inside a function call in python? Something like
curr= []
cur开发者_如何学JAVAr.append(num = num/2)
Nopey. Assignment is a statement. It is not an expression as it is in C derived languages.
I'm pretty certain I remember one of the reasons Python was created was to avoid these abominations, instead preferring readability over supposed cleverness :-)
What, pray tell, is wrong with the following?
curr= []
num = num/2
curr.append(num)
Even if you could, side-effecting expressions are a great way to make your code unreadable, but no... Python interprets that as a keyword argument. The closest you could come to that is:
class Assigner(object):
def __init__(self, value):
self.value = value
def assign(self, newvalue):
self.value = newvalue
return self.value
# ...
num = Assigner(2)
curr = []
curr.append(num.assign(num.value / 2))
精彩评论