Is there a way to do a variable assignment inside a function call in python? Something like
curr= []
curr.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))
;), then note the lexical parallelism to the second sentence of paxdiablo's answer. Then note that the "because I can, but really, don't" of Mr. Safyan's answer and how it isn't anywhere close to the equivalent C-like expression. cf. en.wiktionary.org/wiki/levity