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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:32:40  来源:igfitidea点击:

How do you call a function in a function?

pythonfunction

提问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 squarefunction defined in a squaremodule, then you should look to import the simple name from it instead.

如果且仅当您squaresquare模块中定义了该函数,那么您应该考虑从中导入简单名称。

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.

您有多种选择。

  1. Put everything in one file. Then you only need to call the other function; forget about all imports then.

  2. Put the squarefunction in a different file, e. g. foo.py. Then your using function needs to importit. For that you have two options again: import fooand use foo.square(y)or from foo import squareand use square(y). (You can name your module like your function as well -- the two have separate name spaces -- so then you would have to from square import square.)

  1. 将所有内容放在一个文件中。那么你只需要调用另一个函数;import然后忘记所有的事情。

  2. square函数放在不同的文件中,例如foo.py. 那么你的使用功能需要import它。为此,您再次有两个选择:import fooand 使用foo.square(y)orfrom foo import square和 use square(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 imports.

模块(即在一个单独的文件中)用于将逻辑上连接的事物分组在一起,例如所有数学函数、所有与操作系统相关的事物、所有与随机数生成器相关的事物等等。所以在你的情况下,这看起来像是第一个测试我d 建议将所有内容放在一个文件中,而忘记所有imports。

回答by farhawa

2 way to use a function within an other:

在其他函数中使用函数的 2 种方法:

  1. You define the square()function in another .pyfile (ex: myfile.py) and then, you can import the function this way:
  1. square()在另一个.py文件 ( ex: myfile.py) 中定义该函数,然后可以通过以下方式导入该函数:
from myfile import square

def newFunction():
   square()
  1. You define the function in the same file and then there is no need for the importand you can use square()directly.
  1. 您在同一个文件中定义函数,然后就不需要了import,您可以square()直接使用。

回答by Ty Pavicich

No need for importif its in the same file.

import如果它在同一个文件中,则不需要。

Just call squarefrom the somethingfunction.

只需squaresomething函数中调用。

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)