简体   繁体   English

python:我可以将变量设置为等于自身的函数吗?

[英]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.

相关问题 Python Class - 为什么在设置实例变量时我必须将我的 class 变量设置为等于自身 - Python Class - why do I have to set my class variable equal to itself when setting instance variable Python嵌套函数不等于自身 - Python nested function is not equal to itself 将Python类变量设置为等于类函数的输出 - Set Python class variable equal to the output of class function 如何设置一个变量等于python中函数返回的值 - How do you set a variable equal to the value returned by a function in python 如果变量不等于 python 中的某个数字,我如何制作一个重复自身的“while”循环? - how do I make a "while" loop that repeats itself if a variable isn't equal to a certain number in python? 我可以在Python中同时打印函数并将其设置为变量吗? - Can I print a function and set it to a variable at the same time in Python? 如何使变量等于一个字符串 - Python - How can I make a variable equal more then one string - Python Python:如何在用作参数本身的函数中使用参数? - Python: How can I use arguments in a function used as argument itself? 需要设置一个等于函数一部分结果的变量 - Need to set a variable equal to the result of a part of a function 如何在Python中为特定字符串覆盖完全相等的函数? - How can i override the exactly equal function for a specific string in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM