您如何在函数中调用函数? [英] How do you call a function in a function?

查看:166
本文介绍了您如何在函数中调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,并且正在制作另一个需要调用第一个函数的函数.我没有使用Python的经验,但是我知道在MATLAB这样的语言中,只要它们位于同一目录中就可以.

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.

一个基本示例:

def square(x):
    square = x * x

(并保存)

现在在我的新函数中,我想使用我尝试过的函数平方:

Now in my new function I want to use the function square I tried:

def something (y, z)
  import square
  something = square(y) + square(z)
  return something

显示为:builtins.TypeError: 'module' object is not callable.

我该怎么办?

推荐答案

如果import在同一文件中,则不需要.

No need for import if its in the same file.

只需从something函数调用square.

def square(x):
  square = x * x
  return square

def something (y, z)
  something = square(y) + square(z)
  return something

更简单:

def square(x):
  return x * x

def something (y, z)
  return square(y) + square(z)

这篇关于您如何在函数中调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆