为什么我的python函数没有执行? [英] Why is my python function not being executed?

查看:335
本文介绍了为什么我的python函数没有执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本,该脚本具有缩进的气质,因此我决定编写函数.我对python来说还很陌生,现在我创建了这些功能就没用了!

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!

def main ():
    wiki_scrape()
    all_csv()
    wiki_set = scraped_set('locations.csv')
    country_set = all_set('all.csv')
    print wiki_set

我只是想知道这是否是从main()函数调用函数的正确方法?我一直在争论是否在调用的函数中出现了缩进问题,即使它没有出现错误,python似乎也非常依赖正确的缩进!

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!

完整代码- http://pastebin.com/gJGdHLgr

推荐答案

听起来您需要这样做:

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

更好:

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

内置变量__name__是当前上下文名称空间.如果从命令行运行脚本,则该脚本等效于'__main__'.如果您从其他地方(包括在解释器内部)将.py文件作为模块运行/导入,则名称空间(在模块上下文内)将是.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:

## 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

您将获得:

__name__ is __main__
Hello World!

但是,如果从解释器导入它,则可以看到__name__不是__main__:

If you import it from the interpreter, however, you can see that __name__ is not __main__:

>>> from my_file import *
>>> __name__ is my_file

这篇关于为什么我的python函数没有执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆