在 Python 中“调用”一个函数是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19130958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does it mean to "call" a function in Python?
提问by user2837438
What does "call" mean and do? How would you "call" a function in Python?
“呼叫”是什么意思和做什么?你将如何在 Python 中“调用”一个函数?
采纳答案by Joel Green
When you "call" a function you are basically just telling the program to execute that function. So if you had a function that added two numbers such as:
当您“调用”一个函数时,您基本上只是告诉程序执行该函数。因此,如果您有一个将两个数字相加的函数,例如:
def add(a,b):
return a + b
you would call the function like this:
你会像这样调用函数:
add(3,5)
which would return 8. You can put any two numbers in the parentheses in this case. You can also call a function like this:
这将返回 8。在这种情况下,您可以将任意两个数字放在括号中。您还可以调用这样的函数:
answer = add(4,7)
Which would set the variable answer equal to 11 in this case.
在这种情况下,这会将变量 answer 设置为等于 11。
回答by queso
To "call" means to make a reference in your code to a function that is written elsewhere. This function "call" can be made to the standard Python library (stuff that comes installed with Python), third-party libraries (stuff other people wrote that you want to use), or your own code (stuff you wrote). For example:
“调用”意味着在您的代码中引用在别处编写的函数。可以对标准 Python 库(随 Python 一起安装的内容)、第三方库(其他人编写的您想使用的内容)或您自己的代码(您编写的内容)进行此函数“调用”。例如:
#!/usr/env python
import os
def foo():
return "hello world"
print os.getlogin()
print foo()
I created a function called "foo" and calledit later on with that print statement. I imported the standard "os" Python library then I calledthe "getlogin" function within that library.
我创建了一个名为“foo”的函数,稍后使用该打印语句调用它。我导入了标准的“os”Python 库,然后在该库中调用了“getlogin”函数。
回答by Haresh K Miriyala
when you invoke a function , it is termed 'calling' a function . For eg , suppose you've defined a function that finds the average of two numbers like this-
当您调用一个函数时,它被称为“调用”一个函数。例如,假设您已经定义了一个函数来查找两个数字的平均值,如下所示-
def avgg(a,b) :
return (a+b)/2;
now, to call the function , you do like this .
现在,要调用该函数,您可以这样做。
x=avgg(4,6)
print x
value of x will be 5 .
x 的值为 5 。
回答by Shashank
I'll give a slightly advanced answer. In Python, functions are first-class objects. This means they can be "dynamically created, destroyed, passed to a function, returned as a value, and have all the rights as other variables in the programming language have."
我会给出一个稍微高级的答案。在 Python 中,函数是一等对象。这意味着它们可以“动态创建、销毁、传递给函数、作为值返回,并且拥有编程语言中其他变量所拥有的所有权限”。
Calling a function/class instance in Python means invoking the __call__
method of that object. For old-style classes, class instances are also callable but only if the object which creates them has a __call__
method. The same applies for new-style classes, except there is no notion of "instance" with new-style classes. Rather they are "types" and "objects".
在 Python 中调用函数/类实例意味着调用该__call__
对象的方法。对于旧式类,类实例也是可调用的,但前提是创建它们的对象具有__call__
方法。这同样适用于新式类,除了新式类没有“实例”的概念。相反,它们是“类型”和“对象”。
As quoted from the Python 2 Data Model page, for function objects, class instances(old style classes), and class objects(new-style classes), "x(arg1, arg2, ...)
is a shorthand for x.__call__(arg1, arg2, ...)
".
正如从Python 2 数据模型页面引用的那样,对于函数对象、类实例(旧样式类)和类对象(新样式类),“x(arg1, arg2, ...)
是”的简写x.__call__(arg1, arg2, ...)
。
Thus whenever you define a function with the shorthand def funcname(parameters):
you are really just creating an object with a method __call__
and the shorthand for __call__
is to just name the instance and follow it with parentheses containing the arguments to the call. Because functions are first class objects in Python, they can be created on the fly with dynamic parameters (and thus accept dynamic arguments). This comes into handy with decorator functions/classes which you will read about later.
因此,每当您使用速记定义函数时,def funcname(parameters):
您实际上只是创建了一个带有方法的对象,__call__
而速记__call__
只是命名实例并在它后面加上包含调用参数的括号。因为函数是 Python 中的第一类对象,它们可以使用动态参数动态创建(因此接受动态参数)。这对装饰器函数/类很方便,您将在稍后阅读。
For now I suggest reading the Official Python Tutorial.
现在我建议阅读官方 Python 教程。