[英]python: can i set a variable to equal a function of itself?
Can I do this? 我可以这样做吗?
var1 = some_function(var1)
When I tried to do this I got errors, but perhaps I was doing something wrong. 当我尝试这样做时,我遇到了错误,但也许我做错了。
If the variable has been previously defined, you can do that yes. 如果先前已定义变量,则可以这样做。
Example: 例:
def f(x):
return x+1
var1 = 5
var1 = f(var1)
# var1 is now 6
If the variable has not been defined previously, you can't do that, simply because there is no value that could be passed to the function. 如果先前未定义变量,则不能这样做,因为没有可传递给函数的值。
def myfun(param):
return param * 2
y = 4
y = myfun(y)
print (y)
Works fine. 工作正常。 prints
8
; 打印
8
;
So you could describe better the problem you're experiencing, preferably with full error traceback and source code I can run to reproduce the problem. 因此,您可以更好地描述您遇到的问题,最好使用完整的错误回溯和源代码,我可以运行以重现问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.