Python 如何在函数中调用函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32958704/
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
How do you call a function in a function?
提问by Jane.0
I have a function and I'm making another one in which I need to call the first function. I don't have experience in Python, but I know that in languages like MATLAB it's possible as long as they're in the same directory.
我有一个函数,我正在制作另一个需要调用第一个函数的函数。我没有 Python 方面的经验,但我知道在像 MATLAB 这样的语言中,只要它们在同一目录中就可以了。
A basic example:
一个基本的例子:
def square(x):
square = x * x
(and saved)
(并保存)
Now in my new function I want to use the function square I tried:
现在在我的新函数中,我想使用我尝试过的函数 square:
def something (y, z)
import square
something = square(y) + square(z)
return something
Which displays: builtins.TypeError: 'module' object is not callable
.
其中显示:builtins.TypeError: 'module' object is not callable
。
What should I do?
我该怎么办?
回答by Makoto
If, and only if, you have the square
function defined in a square
module, then you should look to import the simple name from it instead.
如果且仅当您square
在square
模块中定义了该函数,那么您应该考虑从中导入简单名称。
from square import square
If you don't want to change anything, then you need to use its fully qualified name:
如果您不想更改任何内容,则需要使用其完全限定名称:
something = square.square(y) + square.square(z)
The module's name is square
, and you can't call functions on modules.
模块的名称是square
,并且您不能在模块上调用函数。
回答by Alfe
You have several options.
您有多种选择。
Put everything in one file. Then you only need to call the other function; forget about all
import
s then.Put the
square
function in a different file, e. g.foo.py
. Then your using function needs toimport
it. For that you have two options again:import foo
and usefoo.square(y)
orfrom foo import square
and usesquare(y)
. (You can name your module like your function as well -- the two have separate name spaces -- so then you would have tofrom square import square
.)
将所有内容放在一个文件中。那么你只需要调用另一个函数;
import
然后忘记所有的事情。将
square
函数放在不同的文件中,例如foo.py
. 那么你的使用功能需要import
它。为此,您再次有两个选择:import foo
and 使用foo.square(y)
orfrom foo import square
和 usesquare(y)
。(你也可以像你的函数一样命名你的模块——两者有单独的命名空间——所以你必须这样做from square import square
。)
Modules (i. e. in a separate file) are used for grouping logically connected things together, e. g. all mathematical functions, all operating-system related things, all random number generator related things, etc. So in your case which looks like a first test I'd propose to put everything in one file and forget about all import
s.
模块(即在一个单独的文件中)用于将逻辑上连接的事物分组在一起,例如所有数学函数、所有与操作系统相关的事物、所有与随机数生成器相关的事物等等。所以在你的情况下,这看起来像是第一个测试我d 建议将所有内容放在一个文件中,而忘记所有import
s。
回答by farhawa
2 way to use a function within an other:
在其他函数中使用函数的 2 种方法:
- You define the
square()
function in another.py
file (ex: myfile.py
) and then, you can import the function this way:
- 您
square()
在另一个.py
文件 (ex: myfile.py
) 中定义该函数,然后可以通过以下方式导入该函数:
from myfile import square
def newFunction():
square()
- You define the function in the same file and then there is no need for the
import
and you can usesquare()
directly.
- 您在同一个文件中定义函数,然后就不需要了
import
,您可以square()
直接使用。
回答by Ty Pavicich
No need for import
if its in the same file.
import
如果它在同一个文件中,则不需要。
Just call square
from the something
function.
只需square
从something
函数中调用。
def square(x):
square = x * x
return square
def something (y, z)
something = square(y) + square(z)
return something
More simply:
更简单:
def square(x):
return x * x
def something (y, z)
return square(y) + square(z)