I am trying to return (execute) a function from another file in an if statement. 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.
The function creates a sandbox but if one exists I want to pass the if statement.
This is a small snippet of c开发者_StackOverflow社区ode 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.
If foo.py contains this:
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.
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. 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
# 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
import my_module
x = my_module
x.main()
You probably need to import
the module which contains the function, no?
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"?
You can import
the function from the other file and call it like a local function.
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
? Say, your external function lives in mymodule.py in the same directory, you have to import it first:
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)
#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
def outsidefunction(num):
return num * 2
Command-Line: python file1.py
精彩评论