简体   繁体   English

什么决定运行哪个python类的main()

[英]What decides which python class' main() is run

I'm trying to make a tool that displays parameterized data on a dedicated monitor. 我正在尝试制作一个在专用监视器上显示参数化数据的工具。

Following what I've learned in java, I name the method containing the "calling" code main() . 按照我在Java中学到的东西,我将命名包含“调用”代码main()的方法命名。 I then call this main() at the end of the file. 然后,我在文件末尾将此main()调用。 The problem is, for some reason when I run python file1 in the command prompt, it runs the main from file2. 问题是由于某种原因,当我在命令提示符下运行python file1时,它从file2运行了main。 File1 imports file 2, but since avoided the namespace collision over main() by not writing from file2 import* , I'm not sure it's running the other file's main() . File1导入文件2,但是由于通过不from file2 import*写入避免了main()的命名空间冲突,因此我不确定它是否正在运行另一个文件的main()

I'm running windows 7 Ultimate, python 3.4 我正在运行Windows 7 Ultimate,python 3.4

Look into using the __name__ variable to specify which main() you want to run. 考虑使用__name__变量来指定要运行的main()

For an example, check out: What does if __name__ == "__main__": do? 例如,请查看: __name__ ==“ __main__”怎么办?

The main is just a Python variable that points to the function object. main只是指向函数对象的Python变量。 If you assign the main a different value, you loose the reference to the original function. 如果给main电源分配一个不同的值,则您将失去对原始功能的引用。 If you define the main after the import, then your own main will be used. 如果您在导入后定义了main ,则将使用您自己的main

Update: If both file1.py and file2.py contain the definition of the main() function and if both call it in the file -- say as the last line in its own file, and if the file1.py does import file2 --but does not from file2 import * after the main() definition in the file1.py --then both main() functions will be called. 更新:如果file1.pyfile2.py都包含main()函数的定义,并且如果两者都在文件中调用了它,则将其作为自己文件的最后一行,如果file1.py确实import file2file1.py -但是在file1.pymain()定义之后不会from file2 import *然后将调用这两个main()函数。 The file2.main() is called during the import file2 (the first import in the application only), and the file1.main() is going to be called when the main() call in file1.py is found. import file2期间将调用file2.main() (仅在应用程序中是第一次导入),并且在file1.main()main()调用时将调用file1.py main()

Whenever xxx.py is launched as a script, it is processed in the order of the source text. 每当将xxx.py作为脚本启动时,都会按照源文本的顺序对其进行处理。 Processing of a definition means compilation of the definition, when a command is found during the first read, it is executed (actually after compilation to xxx.pyc first). 定义的处理意味着对定义进行编译,在第一次读取过程中发现命令时,将执行该命令(实际上是先编译为xxx.pyc之后)。

The only way to avoid execution of a code in your file is to jump over the code fragment using if with suitable condition. 避免在文件中执行代码的唯一方法是使用if条件合适的代码来跳过代码片段。 This is why the pattern 这就是为什么模式

if __name__ == '__main__':
    main()

is used in the sources. 在来源中使用。 (Which actually is the execution of the code -- only the branch of the if is skipped.) Read it... If the file is launched as a script, then the __name__ variable takes the string value '__main__' , the condition holds and the main() from this file is called. (实际上是代码的执行-仅跳过if的分支。)读取...如果文件作为脚本启动,则__name__变量采用字符串值'__main__' ,条件成立并调用该文件中的main()

If the same pattern is used in the file2.py , and the file is used as a module (that is import file2 ), then the __name__ variable contains the name of the imported module (here file2 ), and the condition does not hold. 如果在file2.py使用了相同的模式,并且该文件用作模块(即import file2 ),则__name__变量包含导入模块的名称(此处为file2 ),并且该条件不成立。 Because of that, the main() from inside file2.py is not called. 因此,不会调用file2.py内部的main()

The pattern is often used even in the files that are expected to be used as modules only. 即使在预期仅用作模块的文件中也经常使用该模式。 The code is usually used for testing the basic functionality of the module. 该代码通常用于测试模块的基本功能。 Think about the situation when the author of the module makes some changes. 考虑模块的作者进行一些更改时的情况。 Then he/she launches it as a script (which is not usually doney), and the body starts testing of the module and reports the result -- say using unittest module or whatever kind of testing. 然后,他/她将其作为脚本启动(通常并不困难),然后主体开始对该模块进行测试并报告结果-例如使用unittest模块或进行任何类型的测试。 So, the file2.main() would be a good place to activate unit testing, for example. 因此,例如, file2.main()将是激活单元测试的好地方。

Try to add the following to the end of your file1.py : 尝试将以下内容添加到file1.py

print(__name__)
print(file2.__name__)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在/ usr / bin中为我的Snow Leopard安装了这么多Python? 是什么决定哪个是System Python? - Why is there so many Pythons installed in /usr/bin for my Snow Leopard? What decides which one is the System Python? 运行python文件 - 什么功能是主要的? - Run python file — what function is main? 在python中定义一个主要函数,该函数可以在python中运行其他函数 - Define a main function in python which can run other functions in python Python 线程:哪些函数在哪个线程中运行 - Python Threading: What functions run in which thread 在命令行/批处理文件/python脚本中运行java main class - Run java main class in command line/batch file/python script 如何运行给定的 Python 脚本与 class、初始化和主从 cmd? - How to run given Python script with class, init and main from cmd? 使用其相对路径运行Java主类? (蟒蛇) - Run a Java main class using its relative path? (Python) 为什么系统只通过扫描第一个数字来决定哪个更大 - Python - why does the system decides which is bigger by scanning only the first number - Python 单击主类中的按钮时,如何运行整个子类 - How can i run a whole sub class when the buttons are clicked which are in the main class Python IDLE:运行main? - Python IDLE: Run main?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM