Why use def main()?

Possible Duplicate: What does if __name__== "__main__" do? I've seen some code samples and tutorials that use def main(): # my code here if __name__ == "__main__": main() But why? Is there any reason not do define your functions at the top of the file, then just write code under it? ie def my_function() # my code here def my_function_two() # my code here

为什么使用def main()?

可能重复: if __name__== "__main__"做什么? 我见过一些使用的代码示例和教程 def main(): # my code here if __name__ == "__main__": main() 但为什么? 有什么理由不在文件顶部定义你的函数,然后在它下面编写代码? 即 def my_function() # my code here def my_function_two() # my code here # some code # call function # print(something) 我只是想知道主音是否有韵? 如果没

server python and file opening error

I am starting in python and developing a client / server script with some input parameters in terminal,arguments:(in) (out) (ip) (port) (mode) according to the code below.In passive mode, it functions as a server, in active mode it functions as a client. But I do not understand the reason for the errors presented. Can someone explain to me what's going on? Thanks. code def Main():

服务器python和文件打开错误

我从python开始,根据下面的代码在终端,参数:(in)(out)(ip)(port)(mode)中开发一个输入参数的客户/服务器脚本。在被动模式下,它作为服务器,在主动模式下它起着客户端的作用。 但我不明白提供错误的原因。 有人可以向我解释发生了什么事吗? 谢谢。 码 def Main(): mode = sys.argv[5] i = 0 # Auxiliar variable in loop if (mode == "passive"): port = int(sys.argv[4])

How to pass parameters to python function using argparse?

I have a foo.py using argparse to get command line parameters for the main() function. """ foo.py """ import argparse def main(): parser = argparse.ArgumentParser(description='test') parser.add_argument('--all', '-a', action='store_true', help='all') args = parser.parse_args() if args.all: pass if __name__ == '__main__': main() And I have to test this main() fun

如何使用argparse将参数传递给python函数?

我有一个使用argparse的foo.py来获取main()函数的命令行参数。 """ foo.py """ import argparse def main(): parser = argparse.ArgumentParser(description='test') parser.add_argument('--all', '-a', action='store_true', help='all') args = parser.parse_args() if args.all: pass if __name__ == '__main__': main() 我必须在另一个Python脚本bar.py上测试这个main()函数。 我

install ns3.21 in ubuntu16.04 error

When I try to "sudo apt-get install gsl-bin libgsl0-dev libgsl0ldbl" 16.04 cannot find the package libgsl0ldbl for the reason that libgsl0-dev is replaced by libgsl-dev and libgsl0ldbl is obslolete, now replaced by libgsl2. I don't know if it matters. When I run the command "./build.py –enable-examples –enable-tests". It failed with the message : Build NS-3 Enterin

在ubuntu16.04错误中安装ns3.21

当我尝试“sudo apt-get install gsl-bin libgsl0-dev libgsl0ldbl”时,由于libgsl0-dev被libgsl-dev替换而libgsl0ldbl变为obslolete,现在被libgsl2替换,因此无法找到软件包libgsl0ldbl。 我不知道它是否重要。 当我运行“./build.py -enable-examples -enable-tests”命令时。 它失败的消息: 建立NS-3 进入目录./ns-3.25' Traceback (most recent call last):文件“./build.py”,线路171,在sys.exit(主(sys.argv

How to call one function from within another function in python

this may seem like a very basic question, but i have difficulty grasping it, and would appreciate any help. I want to be able to call CheckForJiraIssueRecord from verify_commit_text . here is the code: when i run it i get error: jira ticket regex matched! printing m.group(1) QA-65 my_args ... QA-65 transaction abort! rollback completed abort: pretxncommit.jira hook exited with status 1 mea

如何从python的另一个函数中调用一个函数

这可能看起来像一个非常基本的问题,但我很难理解它,并希望得到任何帮助。 我希望能够从verify_commit_text调用CheckForJiraIssueRecord 。 这里是代码:当我运行它我得到错误: jira ticket regex matched! printing m.group(1) QA-65 my_args ... QA-65 transaction abort! rollback completed abort: pretxncommit.jira hook exited with status 1 这意味着,CheckForJiraIssueRecord(my_args)只是没有被调用 #!/u

What is the purpose of self?

What is the purpose of the self word in Python? I understand it refers to the specific object created from that class, but I can't see why it explicitly needs to be added to every function as a parameter. To illustrate, in Ruby I can do this: class myClass def myFunc(name) @name = name end end Which I understand, quite easily. However in Python I need to include self : c

自我的目的是什么?

Python中self单词的目的是什么? 我知道它指的是从该类创建的特定对象,但我不明白为什么它明确需要作为参数添加到每个函数中。 为了说明,在Ruby中我可以这样做: class myClass def myFunc(name) @name = name end end 我很容易理解。 然而在Python中我需要包含self : class myClass: def myFunc(self, name): self.name = name 任何人都可以通过这个说说我吗 这不是我在我的(当然是有

How to process python generated error messages my own way?

For some code as follows, opts, args = getopt.getopt(sys.argv[1:], "c:", ... for o,v in opts: ... elif o in ("-c", "--%s" % checkString): kCheckOnly = True clientTemp = v If I don't give the parameter after the -c, I get the error messages as follows. Traceback (most recent call last): File "niFpgaTimingViolationMain.py", line 100, in opts,

如何处理python生成的错误消息我自己的方式?

对于如下的一些代码, opts, args = getopt.getopt(sys.argv[1:], "c:", ... for o,v in opts: ... elif o in ("-c", "--%s" % checkString): kCheckOnly = True clientTemp = v 如果我在-c之后没有给出参数,我会收到如下错误消息。 Traceback (most recent call last): File "niFpgaTimingViolationMain.py", line 100, in opts, args = getopt.getopt(sys.argv[1:], "hdc

How can I find script's directory with Python?

This question already has an answer here: How to properly determine current script directory? 12 answers 你需要在__file__上调用os.path.realpath ,这样当__file__是没有路径的文件名时,你仍然可以得到dir路径: import os print(os.path.dirname(os.path.realpath(__file__))) I use: import os import sys def get_script_path(): return os.path.dirname(os.path.realpath(sys.argv[0])) As aiham points

我如何用Python找到脚本的目录?

这个问题在这里已经有了答案: 如何正确确定当前脚本目录? 12个答案 你需要在__file__上调用os.path.realpath ,这样当__file__是没有路径的文件名时,你仍然可以得到dir路径: import os print(os.path.dirname(os.path.realpath(__file__))) 我用: import os import sys def get_script_path(): return os.path.dirname(os.path.realpath(sys.argv[0])) 正如aiham在评论中指出的那样,您可以在模块中定义此功能

Print in terminal with colors?

How can I output colored text to the terminal, in Python? What is the best Unicode symbol to represent a solid block? This somewhat depends on what platform you are on. The most common way to do this is by printing ANSI escape sequences. For a simple example, here's some python code from the blender build scripts: class bcolors: HEADER = '33[95m' OKBLUE = '33[94m' OKGREEN =

在颜色的终端打印?

我如何在Python中输出彩色文本到终端? 代表固体块的最佳Unicode符号是什么? 这有点取决于你在哪个平台上。 最常见的方法是打印ANSI转义序列。 举一个简单的例子,下面是搅拌机构建脚本中的一些python代码: class bcolors: HEADER = '33[95m' OKBLUE = '33[94m' OKGREEN = '33[92m' WARNING = '33[93m' FAIL = '33[91m' ENDC = '33[0m' BOLD = '33[1m' UNDERLINE = '33[4m' 要使用这样

How do I get the path of the current executed file in Python?

This may seem like a newbie question, but it is not. Some common approaches don't work in all cases: sys.argv[0] This means using path = os.path.abspath(os.path.dirname(sys.argv[0])) , but this does not work if you are running from another Python script in another directory, and this can happen in real life. __file__ This means using path = os.path.abspath(os.path.dirname(__file__))

如何在Python中获取当前执行文件的路径?

这看起来像一个新手问题,但事实并非如此。 一些常用方法在所有情况下都不起作用: sys.argv中[0] 这意味着使用path = os.path.abspath(os.path.dirname(sys.argv[0])) ,但如果您从另一个目录中的另一个Python脚本运行,则这不起作用,并且这可能会在现实生活中发生。 __文件__ 这意味着使用path = os.path.abspath(os.path.dirname(__file__)) ,但我发现这不起作用: py2exe没有__file__属性,但有一个解决方法