我对此有很大的触摸最近,因为我是工作在我的蟒蛇最后的项目。我也会参与查看你的外部函数文件。
如果你正在调用一个模块(实际上,同一个文件之外的任何函数都可以当作一个模块来处理,我讨厌把它指定得太精确),所以你需要确定一些东西。这里是一个模块的例子,让我们把它叫做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__
现在,我想打电话给在另一个文件这个全功能的,被称为work.py
import my_module
x = my_module
x.main()
请澄清 - 是什么情况你在?向我们展示一些代码 – 2009-12-18 15:05:14
对于初学者来说,它并不完全清楚你想做什么 - 你想返回一个在外部文件中定义的函数,返回执行该外部函数返回的值,还是执行外部函数?如何处理一些示例代码? – 2009-12-18 15:05:24
你用这段代码得到了什么错误? – 2009-12-18 15:13:49