关于Python中__main__的概念性查询 [英] Conceptual inquiry about __main__ in Python

查看:117
本文介绍了关于Python中__main__的概念性查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Python,并且对函数列在 __ main __ 中感到困惑。我一直在寻找多个python脚本来试图找到一个共同的主题,以确定哪些函数允许在 __ main __ 中放置一个地方,但无济于事。在这里我有我自己的代码的示例。 firstfunction
anotherfunction 是我的代码中唯一的两个函数。

  def main(argv):

firstinput =
secondinput =

if len(argv)< 3或len(argv)> 3:
print请设置为:
metisfinal.main(metisfinal.py,firstinput,secondinput)
return
else:
firstinput = argv [1]
secondinput = argv [2]

firstfunction(firstinput,dictionary)
anotherfunction(list,secondinput)

if __name__ ==__main__:
main(sys.argv)

(我认为)I知道参数和 __ main __ 调用是正确的,但 firstfunction 和 anotherfunction 总是返回错误(因为它们的参数不是全局定义的)。我确信这是由于对 __ main __ 的错误理解而产生的,因为我查看过的所有其他示例基本上都设置了 __ main __ 以相同的方式。

__ main __ 中列出了一个特定的函数。我偶然发现了一些Python代码,其中有30个以上的函数,但程序员只在 __ main __ 中列出了其中的2个函数。同样,有时代码在主参数中也会有类,就像这个( Project 之前被定义为一个对象类):

  def main(argv):

filename =
outputfilename =

p =项目(filename,outputfilename,subdomainNames)
p.generateICs()

if __name__ ==__main__:
main(sys.argv)

从概念上讲,我无法理解为什么所有的函数都没有列出...不是所有的函数都需要运行或者是 __ main __ 简单地初始化一些东西?



我在看非典型代码吗?我缺少什么 __ main __ 的关键概念?并且一旦我找到要放入 __ main __ 的函数,是否有格式化它们的特定方法?

解决方案

目前尚不清楚 __ main __ 中所列的含义。 __ main __ 不是源文件中的实体。相反,它是模块的名称,如果直接执行它。当你执行 if __name __ ==__ main __时,你告诉Python执行该块中的代码,当且仅当代码作为主模块被执行时 - - 也就是说,如果它是一个正在运行的程序。如果模块是从另一个模块导入的,则 if if __name __ ==__ main __块中的代码将不会运行。



请注意,如果block中没有列出函数。相反,您将常规程序代码放在您想要运行的那个块中。通常这段代码只是调用一个函数。通常人们会调用该函数 main()。但是 __ main __ main 之间没有特别的关系。你可以调用任何你喜欢的函数:
$ b $ pre $ def snicklefritz():
#这个函数会在你运行时运行运行程序
print你运行的程序!

if __name__ ==__main__:
snicklefritz()



<尝试运行该程序(例如,通过将其保存为snicklefritz.py,然后从命令行执行 python snicklefritz.py )。你会看到你运行的程序!打印。如果您创建一个单独的文件,并且 import snicklefritz ,则不会打印该消息。



请注意,没有关于列表功能的内容。例如,看看这个程序:

  print这将始终打印! 

if __name__ ==__main__:
print只有当您将该文件作为程序运行时,才会打印该文件!

这里 if __name __ ==__ main __块不会列出任何功能。它只包含当文件作为脚本运行时运行的实际代码。但是,人们通常不会这样做,因为将代码放在一个单独的函数中而不是只是坐在那里暴露在函数之外会更加整洁。



至于其他功能,您可以在模块中定义您喜欢的任何其他功能,以便在该模块中使用,或由其他导入模块的模块使用。通常,如果__name __ ==__ main __块中的模块中的大部分函数不会在内部使用,因为它们不会成为main函数的一部分。相反,它们将是其他功能,供其他代码使用。例如:

  def otherFunc(x):
#返回x平方
返回x ** 2

def snicklefritz():
#当你运行程序
时,这个函数会运行print你运行的程序!

if __name__ ==__main__:
snicklefritz()



<在模块中完全不使用p> otherFunc 。没关系。可能有人会想要导入你的模块并自己使用 otherFunc 。并非每个函数都必须在同一个模块中使用,更不用说如果__name __ ==__ main __
块被调用


I am currently working with Python and have been confused over the fact that functions are listed in __main__. I have been looking over multiple python scripts to try to find a common theme as to what functions warrant a place in __main__, but to no avail. Here I have a sample of my own code. firstfunction and anotherfunction are the only two functions in my code.

def main(argv):

    firstinput=""
    secondinput=""

    if len(argv) < 3 or len(argv) > 3:
        print """"Please set to:
                metisfinal.main(metisfinal.py, firstinput, secondinput)""""
        return
    else:
        firstinput = argv[1]
        secondinput = argv[2]

    firstfunction(firstinput, dictionary)
    anotherfunction(list, secondinput)

if __name__ == "__main__":
    main(sys.argv)

(I think) I know that the arguments and __main__ call are correct, but firstfunction and anotherfunction always return errors (because their arguments are not globally defined). I'm positive that this is arising from a faulty understanding of __main__, because all the other examples I've looked at, basically set up __main__ in the same manner.

What constitutes listing a specific function in __main__? I have stumbled over some Python code that has upwards of 30 functions within it, but the programmer only listed 2 of those functions in __main__. Likewise, sometimes a code will have classes in the main argument, like this one (Project is earlier defined as an object class):

def main(argv):

    filename = ""
    outputfilename = ""

    p = Project(filename, outputfilename, subdomainNames) 
    p.generateICs()

if __name__ == "__main__":
    main(sys.argv)

Conceptually, I can't understand why all the functions aren't listed... don't all of them need to be run or is __main__ simply initializing something?

Am I looking at atypical code? What key concepts of __main__ am I missing? And once I do find what functions to put into __main__, is there a specific way to format them?

解决方案

It's not clear what you mean by "listed in __main__". __main__ is not an entity in the source file. Rather, it is the name of the module, if you execute it directly. When you do if __name__=="__main__", you are telling Python to execute the code in that block if and only if the code is being executed as the main module --- that is, if it is a program being run. The code in the if __name__=="__main__" block will not be run if the module is imported from another module.

Note that you do not "list" functions in that if block. Instead, you put regular program code in that block that you want to be run. Often this code just calls one function. Often people call that function main(). But there is no special relationship between __main__ and main. You can call the function anything you like:

def snicklefritz():
    # This function will be run when you run the program
    print "You ran the program!"

if __name__ == "__main__":
    snicklefritz()

Try running this program (say, by saving it as "snicklefritz.py" and then doing python snicklefritz.py from the command line). You'll see "You ran the program!" printed. If instead you create a separate file that does import snicklefritz, the message won't be printed.

Note that there's nothing about "listing functions". For example, look at this program:

print "This will always be printed!"

if __name__ == "__main__":
    print "This will only be printed if you run the file as a program!"

Here the if __name__=="__main__" block does not "list" any functions. It just contains actual code that is run when the file is run as a script. People usually don't do this, though, because it's more tidy to have the code in a separate function instead of just sitting there "exposed" outside of a function.

As for other functions, you can define whatever other functions you like in your module, to be used either within that module, or to be used by other modules that import your module. Typically most of the functions in a module won't be used inside the if __name__=="__main__" block, because they won't be part of the "main" function. Instead, they'll be other functions intended for use by other code. For example:

def otherFunc(x):
    # Return x squared
    return x**2

def snicklefritz():
    # This function will be run when you run the program
    print "You ran the program!"

if __name__ == "__main__":
    snicklefritz()

otherFunc is not used at all in the module. That's fine. It may be that someone will want to import your module and use otherFunc themselves. Not every function has to be used within the same module, let alone be called from the if __name__=="__main__" block.

这篇关于关于Python中__main__的概念性查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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