[英]Assigning a function to a variable
Let's say I have a function假设我有一个功能
def x():
print(20)
Now I want to assign the function to a variable called y
, so that if I use the y
it calls the function x
again.现在我想将该函数分配给一个名为
y
的变量,这样如果我使用y
它会再次调用函数x
。 if i simply do the assignment y = x()
, it returns None
.如果我简单地做赋值
y = x()
,它返回None
。
You simply don't call the function.您根本不调用该函数。
>>> def x():
>>> print(20)
>>> y = x
>>> y()
20
The brackets tell Python that you are calling the function, so when you put them there, it calls the function and assigns y
the value returned by x
(which in this case is None
).括号告诉 Python 您正在调用该函数,因此当您将它们放在那里时,它会调用该函数并为
y
分配x
返回的值(在本例中为None
)。
When you assign a function to a variable you don't use the () but simply the name of the function.当您将函数分配给变量时,您不使用 (),而只是使用函数的名称。
In your case given def x(): ...
, and variable silly_var
you would do something like this:在给定
def x(): ...
和变量silly_var
的情况下,您将执行以下操作:
silly_var = x
and then you can call the function either with然后你可以调用这个函数
x()
or或者
silly_var()
when you perform y=x()
you are actually assigning y to the result of calling the function object x
and the function has a return value of None
.当您执行
y=x()
时,您实际上是将 y 分配给调用函数对象x
的结果,并且该函数的返回值为None
。 Function calls in python are performed using ()
. python 中的函数调用是使用
()
执行的。 To assign x to y
so you can call y
just like you would x
you assign the function object x to y like y=x
and call the function using y()
要将
x to y
以便您可以像调用 x 一样调用y
,您可以像y=x
一样将函数对象x
分配给 y 并使用y()
调用该函数
The syntax语法
def x():
print(20)
is basically the same as x = lambda: print(20)
(there are some differences under the hood, but for most pratical purposes, the results the same).与
x = lambda: print(20)
基本相同(引擎盖下存在一些差异,但对于大多数实际目的,结果相同)。
The syntax语法
def y(t):
return t**2
is basically the same as y= lambda t: t**2
.与
y= lambda t: t**2
基本相同。 When you define a function, you're creating a variable that has the function as its value.当你定义一个函数时,你正在创建一个以函数为值的变量。 In the first example, you're setting
x
to be the function lambda: print(20)
.在第一个示例中,您将
x
设置为函数lambda: print(20)
。 So x
now refers to that function.所以
x
现在指的是那个函数。 x()
is not the function, it's the call of the function. x()
不是函数,它是函数的调用。 In python, functions are simply a type of variable, and can generally be used like any other variable.在 python 中,函数只是一种变量,通常可以像任何其他变量一样使用。 For example:
例如:
def power_function(power):
return lambda x : x**power
power_function(3)(2)
This returns 8. power_function
is a function that returns a function as output.这将返回 8。
power_function
是一个返回函数作为输出的函数。 When it's called on 3
, it returns a function that cubes the input, so when that function is called on the input 2
, it returns 8. You could do cube = power_function(3)
, and now cube(2)
would return 8.当它在
3
上调用时,它返回一个对输入进行立方处理的函数,因此当在输入2
上调用该函数时,它返回 8。您可以执行cube = power_function(3)
,现在cube(2)
将返回 8。
lambda should be useful for this case. lambda 应该对这种情况有用。 For example,
例如,
create function y=x+1 y=lambda x:x+1
创建函数 y=x+1
y=lambda x:x+1
call the function y(1)
then return 2
.调用函数
y(1)
然后返回2
。
I don't know what is the value/usefulness of renaming a function and call it with the new name.我不知道重命名函数并用新名称调用它的价值/用处是什么。 But using a string as function name, eg obtained from the command line, has some value/usefulness:
但是使用字符串作为函数名,例如从命令行获得的,有一些价值/用处:
import sys
fun = eval(sys.argv[1])
fun()
In the present case, fun = x.在本例中,乐趣 = x。
def x():
print(20)
return 10
y = x
y()
print(y)
gives the output给出输出
20
<function x at 0x7fc548cd3040>
so it does not actually assign the value returned by x() to the variable y.所以它实际上并没有将 x() 返回的值赋给变量 y。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.