为什么我的python函数没有被执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29652264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my python function not being executed?
提问by Gaddi
I have written a script that is pretty temperamental with indentation so I decided to make functions. I'm pretty new to python and now that I've created these functions nothing works!
我写了一个带有缩进的脚本,所以我决定制作函数。我对 python 很陌生,现在我已经创建了这些函数,但没有任何作用!
def main ():
wiki_scrape()
all_csv()
wiki_set = scraped_set('locations.csv')
country_set = all_set('all.csv')
print wiki_set
I'm just wondering if this is the correct way to call functions from the main() function? I've been debating if an indentation issue is occurring within the called functions, python seems to be very reliant on proper indentations even though it doesn't come up with an error!
我只是想知道这是否是从 main() 函数调用函数的正确方法?我一直在争论是否在被调用的函数中发生了缩进问题,python 似乎非常依赖正确的缩进,即使它没有出现错误!
Full Code - http://pastebin.com/gJGdHLgr
完整代码 - http://pastebin.com/gJGdHLgr
采纳答案by Rick supports Monica
It sounds like you need to do this:
听起来你需要这样做:
def main():
wiki_scrape()
all_csv()
wiki_set = scraped_set('locations.csv')
country_set = all_set('all.csv')
print wiki_set
main() # this calls your main function
Even better:
更好的是:
def main():
wiki_scrape()
all_csv()
wiki_set = scraped_set('locations.csv')
country_set = all_set('all.csv')
print wiki_set
if __name__ == '__main__':
main() # this calls your main function
Then run it from the command line like this:
然后像这样从命令行运行它:
python file_name.py
The built-in variable __name__
is the current contextual namespace. If you run a script from the command line, it will be equivalent to '__main__'
. If you run/import the .py file as a module from somewhere else, including from inside the interpreter, the namespace (inside of the context of the module) will be the .py file name, or the package name if it is part of a package. For example:
内置变量__name__
是当前上下文命名空间。如果您从命令行运行脚本,它将等效于'__main__'
. 如果您从其他地方(包括从解释器内部)将 .py 文件作为模块运行/导入,则命名空间(在模块的上下文内部)将是 .py 文件名,或者是包名(如果它是其中的一部分)一袋。例如:
## my_file.py ##
print('__name__ is {0}'.format(__name__))
if __name__ = '__main__':
print("Hello World!")
If you do this from command line:
如果您从命令行执行此操作:
python my_file.py
You will get:
你会得到:
__name__ is __main__
Hello World!
If you import it from the interpreter, however, you can see that __name__
is not __main__
:
但是,如果您从解释器中导入它,您可以看到它__name__
不是__main__
:
>>> from my_file import *
>>> __name__ is my_file
回答by NDevox
Python doesn't call any functions on starting unless explicitly asked to (including main
).
除非明确要求(包括main
),否则 Python 在启动时不会调用任何函数。
Instead Python names the files being run, with the main file being run called __main__
.
相反,Python 将正在运行的文件命名为__main__
.
If you want to simply call the main function you can use Rick's answer.
如果您只想调用 main 函数,您可以使用 Rick 的回答。
However in Python best practice it is better to do the following:
但是,在 Python 最佳实践中,最好执行以下操作:
if __name__ == '__main__':
wiki_scrape()
all_csv()
wiki_set = scraped_set('locations.csv')
country_set = all_set('all.csv')
print wiki_set
This ensures that if you are running this python file as the main file (the file you click on, or run from the cmd) then these functions will be run.
这确保如果您将此 python 文件作为主文件(您单击的文件或从 cmd 运行的文件)运行,则将运行这些函数。
If this is instead used as an imported module, you can still use the functions in the script importing the module, but they will not be called automatically and thus won't interfere with the calling script.
如果将其用作导入模块,您仍然可以使用导入模块的脚本中的函数,但它们不会被自动调用,因此不会干扰调用脚本。