简体   繁体   English

在python中调用外部函数

[英]calling an outside function in python

I am trying to return (execute) a function from another file in an if statement. 我试图从if语句中的另一个文件返回(执行)函数。 I have read that the return statement will not work, I was hoping someone would know what statement would allow me to call an outside function. 我读到return语句将不起作用,我希望有人会知道哪种语句将允许我调用外部函数。

The function creates a sandbox but if one exists I want to pass the if statement. 该函数创建一个沙箱,但如果存在,我想传递if语句。

This is a small snippet of code I used. 这是我使用的一小段代码。

import mks_function  
from mksfunction import mks_create_sandbox  
import sys, os, time  
import os.path  

if not os.path.exists('home/build/test/new_sandbox/project.pj'):
 return mks_create_sandbox()  
else:  
 print pass  

Say your function bar is in a file called foo.py on your Python path. 假设您的功能bar位于Python路径上名为foo.py的文件中。

If foo.py contains this: 如果foo.py包含以下内容:

def bar():
  return True

Then you can do this: 然后,您可以执行以下操作:

from foo import bar

if bar():
  print "bar() is True!"

let's see what docs say : 让我们看看文档怎么说

return may only occur syntactically nested in a function definition, not within a nested class definition. return只能在语法上嵌套在函数定义中,而不能在嵌套的类定义中。

what you're trying to do, I guess is: 我想做的是:

from mksfunction import mks_create_sandbox  
import os.path

if not os.path.exists('home/build/test/new_sandbox/project.pj'):
    mks_create_sandbox()

I have a big touch on this recently as I was working on my final project in python. 我最近在python的最终项目中工作时,对此有很大的了解。 I would be engaged to look at your outside function file too. 我也将致力于查看您的外部功能文件。

If you are calling a module (well actually, any function outside the same file can be treated as a module, I hate to specify things too precise), you need to make sure something. 如果要调用模块(实际上,同一文件之外的任何函数都可以视为模块,我讨厌指定过于精确的东西),则需要确保某些内容。 Here is an example of a module, let's called it my_module.py 这是一个模块的示例,我们称它为my_module.py

# Example python module

import sys
# Any other imports... imports should always be first

# Some classes, functions, whatever...
# This is your meat and potatos

# Now we'll define a main function
def main():
    # This is the code that runs when you are running this module alone
    print sys.platform

# This checks whether this file is being run as the main script
#  or if its being run from another script
if __name__ == '__main__':
    main()
# Another script running this script (ie, in an import) would use it's own
#  filename as the value of __name__

Now I want to call this entire function in another file, called work.py 现在,我想在另一个名为work.py的文件中调用整个函数。

import my_module

x = my_module
x.main()

You probably need to import the module which contains the function, no? 您可能需要import包含功能的模块,不是吗?

Of course, a little more precision as to what you are trying to achieve would help. 当然,对您要实现的目标稍加精确将有所帮助。

What exactly do you mean by "the return statement will not work"? “ return语句将不起作用”到底是什么意思?

You can import the function from the other file and call it like a local function. 您可以从其他文件import函数,然后像本地函数一样调用它。

It depends what you mean. 这取决于你的意思。 If you want to create a static method then you would do something like 如果要创建静态方法,则可以执行以下操作

class fubar(object):

    @classmethod
    def foo():
      return bar

fubar.foo() # returns bar

If you want to run an external process then you would use subprocess library and do 如果要运行外部流程,则可以使用子流程库并执行

import subprocess
subprocess.popen("cmd echo 'test'",shell=true)

really depends what you want to do 真的取决于你想做什么

Do you mean import ? 你是说import吗? Say, your external function lives in mymodule.py in the same directory, you have to import it first: 说,您的外部函数位于同一目录中的mymodule.py中,您必须首先将其导入:

import mymodule
# or
from mymodule import myfunction

Then it is straight forward to use the function: 然后直接使用该函数:

if mymodule.myfunction() == "abc":
    # do something

or with the second import: 或第二次导入:

if myfunction() == "abc":
    # do something

See this tutorial . 请参阅本教程

file1.py (comment out 2 of the versions) file1.py(注释2个版本)

#version 1
from file2 import outsidefunction
print (outsidefunction(3))

#version 2
import file2
print (file2.outsidefunction(3))

#version 3
from file2 import *
print (outsidefunction(3))

file2.py file2.py

def outsidefunction(num):
    return num * 2

Command-Line: python file1.py 命令行: python file1.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM