I was wondering if it is possible to create a function foo in python so that
def calulate (self, input):
input = #some stuff
def foo2(self):
self.calculate(self.var1)
self.calculate(self.var2)
or do you have to do this
def calculation(self):
output=#some stuff
return output
def foovar1(self):
self.var1=self.calculation()
self.var2=self.calculation()
I really don’t want to have to do this because it would mean creating many more functions
In Python, you can mutate function arguments, but you can’t rebind them in the caller’s scope directly. You could pass the instance member name:
Alternately, if
self.var1is a mutable object e.g. a list you could write:This works because you’re mutating the list object (by assigning to a full slice) rather than rebinding it (a bare
=).