我想知道是否有可能在python中创建一个函数foo以便
def calulate (self, input):
input = #some stuff
def foo2(self):
self.calculate(self.var1)
self.calculate(self.var2)
还是一定要这么做
def calculation(self):
output=#some stuff
return output
def foovar1(self):
self.var1=self.calculation()
self.var2=self.calculation()
我真的不想这样做,因为这意味着要创建更多的函数
另一个解决方案是使用特制的容器。你知道吗
然后在另一个类中使用:
在Python中,可以改变函数参数,但不能直接在调用者的作用域中重新绑定它们。您可以传递实例成员名称:
或者,如果
self.var1
是一个可变对象,例如,您可以编写的列表:这之所以有效,是因为您正在对list对象进行变异(通过分配给一个完整的片),而不是重新绑定它(一个空的
=
)。你知道吗相关问题 更多 >
编程相关推荐