你如何调用函数中的函数?

时间:2015-10-05 22:07:42

标签: python python-3.5

我看到了另外两个这样的问题,但它们没有用......

所以我的问题是,我有一个函数,我正在制作另一个我需要调用第一个函数的函数。我没有Python方面的经验,但我知道只要它们位于同一目录中,就像Matlab这样的语言是可能的。

一个基本的例子:

def square(x):
square = x * x

(并保存)

现在在我的新功能中我想使用函数square 我试过了:

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

显示:bu​​iltins.TypeError:'module'对象不可调用

我该怎么办?

4 个答案:

答案 0 :(得分:3)

如果<button id="gobtn">Make something happen in one second from now</button> <div id="output"></div>位于同一个文件中,则无需{。<}。

只需从b'{"userdetails":[["{\\”user_id\\":[\\”54562af66ffd\\"],\\”user_name\\":[\\"bewwrking\\"],\\”room\\":[\\"31\\”]}' 功能调用import

square

更简单:

something

答案 1 :(得分:2)

如果 ,并且只有在square模块中定义了square函数,那么您应该尝试导入简单从中取名。

from square import square

如果您不想更改任何内容,则需要使用其完全限定名称:

something = square.square(y) + square.square(z)

模块名称为square,您无法调用模块上的函数。

答案 2 :(得分:0)

您有几种选择。

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

  2. square函数放在另一个文件中,例如: G。 foo.py。然后你的使用函数需要import它。为此,您有两个选项:import foo并使用foo.square(y)from foo import square并使用square(y)。 (您可以将您的模块命名为您的函数 - 两者具有单独的名称空间 - 因此您必须from square import square。)

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

答案 3 :(得分:0)

在另一个中使用函数的2种方法:

  1. 您在另一个private String buildQueryString(String url, List<NameValuePair> params) throws IOException { StringBuilder sb = new StringBuilder(); if (params == null) return url; for (NameValuePair param : params) { sb.append(urlEncode(param.getName())); sb.append("="); sb.append(urlEncode(param.getValue())); sb.append("&"); } return url + "?" + sb.substring(0, sb.length() - 1); } 文件(buildscript { repositories { maven { url 'https://maven.fabric.io/public' } maven { url 'https://zendesk.artifactoryonline.com/zendesk/repo' } } dependencies { classpath 'io.fabric.tools:gradle:1.+' } } apply plugin: 'com.android.application' apply plugin: 'io.fabric' repositories { maven { url 'https://maven.fabric.io/public' } } android { signingConfigs { debug { storeFile file('keystore/debug/debug.keystore') } } compileSdkVersion 23 buildToolsVersion "21.1.2" useLibrary 'org.apache.http.legacy' // jenkins setup def versionPropsFile = file('version.properties') def code = 1; if (versionPropsFile.canRead() && versionPropsFile.exists()) { def Properties versionProps = new Properties() versionProps.load(new FileInputStream(versionPropsFile)) List<String> runTasks = gradle.startParameter.getTaskNames(); def value = 0 for (String item : runTasks) { if (item.contains("assembleRelease")) { value = 1; } } code = Integer.parseInt(versionProps['VERSION_CODE']).intValue() + value versionProps['VERSION_CODE'] = code.toString() versionProps.store(versionPropsFile.newWriter(), null) } else { throw new GradleException("Could not read version.properties!") } // construct version name def versionMajor = 2 def versionMinor = 3 def versionPatch = 9 defaultConfig { minSdkVersion 16 targetSdkVersion 22 versionName "${versionMajor}.${versionMinor}.${versionPatch}" versionCode code signingConfig signingConfigs.debug // enabling multidex support multiDexEnabled true } buildTypes { stage { <content removed> } debug { <content removed> } release { <content removed> } } packagingOptions { exclude 'META-INF/LICENSE.txt' } dependencies { <bunch of dependencies> compile 'com.android.support:support-v4:23.0.1' } )中定义square()函数,然后,您可以通过这种方式导入函数:
  2. .py
    1. 您可以在同一个文件中定义该功能,但不需要ex: myfile.py,您可以直接使用from myfile import square def newFunction(): square()