我正在寻找一种在定义函数后更改函数内部定义的变量的方法。
例如
def GetNthPower(x) :
n = None
return x**n
my_numbers_list = [11,23,45,56,78,98]
# now if I feel like I need the 4th power of some numbers in the list
GetNthPower.n = 4
for x in my_numbers_list :
print GetNthPower(x)
# If I want 7th power then
GetNthPower.n = 7
这显然行不通,有没有办法做到这一点?
注意:我知道我们可以通过将“n”设置为函数的参数来实现这一点,但出于特定原因我想这样做。我希望我的函数只有一个参数(用于在 中使用函数multiprocessing.Pool.map())。