这是什么,为什么应该包括一个 if 声明?

if __name__ == "__main__":
    print("Hello, World!")

如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。


当前回答

当一个 Python 文件运行时,它创建了许多特殊变量,如 __name__. 变量 __name__ 保留了文件的名称。

if __name__ == "__main__":
       # Do something

这意味着,如果执行的文件的名称运行为源文件而不是模块,那么它将运行代码在其中。 这可以通过一个简单的例子证明。 创建两个 Python 文件, foo.py 和 second.py. 然后在 foo.py 中输入此:

if __name__ == "__main__":
       print("file is not imported")
else:
       print("file is imported")

在第二.py 中,输入此:

import foo

if foo.__name__ == "__main__":
       print("file is not imported")
else:
       print("file is imported")

除此之外,如果你要做这个打印(__name__),那么它会打印 __main__。

因为文件是作为主要来源运行,如果你打印(foo.__name__)它将打印 foo 因为默认值的 __name__ 变量是文件的名称,默认情况下我意味着你也可以更改它。

__name__ = "Hello, World!"
print(__name__)

然后,产量将是:

Hello, World!

其他回答

简单的答案是下方写的代码,如果名称 ==“主”:如果您将其输入到另一个文件,则不会执行。

假设我在维基百科上写了网页扫描的功能和课程,当然,这可能不是一个好例子。

我想在另一个程序中使用这些功能,而不重写它。

好吧,我进口它们,但在该文件的结尾我把 __name__ == '__main__'

当我们进口一个模块时,它内部的所有代码都从开始到结束,但当它达到条件时,它不运行函数、函数2等,即维基百科 __scrape__。

好吧,在全球范围内,Python __name__ 为当前程序定义为“__main__”。

当我们进口一个模块时,它被定义为我们当前程序的名称空间中的变量,而当前程序 __name__ 是“__main__”:

文件测试.py

def func():
    # Do something
    pass

def func2():
    # Do something
    pass

print('The program name is set to ', globals()['__name__'])

if __name__=='__main__':
    # In the current program, __name__ is equal to '__main__'
    func('https://www.wikipedia.org')
    func2('https://www.wikipedia.org')
    # Or do more jobs

import test1
print('inside of current program')
print('name is current program', __name__)
print(globals()['test1'])
test1.func('another site')
test1.func2('another site')

出口

inside of test 1
name of program is set to test1
end of module
inside of current
__main__
<module 'test1' from 'C:\\users\\ir\\appdata\\local\\programs\\python\\python38\\lib\\test1.py'>

您可以通过这个简单的例子查看特殊变量 __name__:

创建 file1.py

if __name__ == "__main__":
    print("file1 is being run directly")
else:
    print("file1 is being imported")

创建 *file2.py

import file1 as f1

print("__name__ from file1: {}".format(f1.__name__))
print("__name__ from file2: {}".format(__name__))

if __name__ == "__main__":
    print("file2 is being run directly")
else:
    print("file2 is being imported")

执行 file2.py

出口:

file1 is being imported
__name__ from file1: file1
__name__ from file2: __main__
file2 is being run directly

Python 主要功能是任何程序的起点. 当程序运行时,Python 翻译器连续运行代码. 主要功能只有当它作为 Python 程序运行时才运行。

def main():
    print("I am in the function")

print("I am out of the function")

当你运行脚本显示:

I am out of the function

而不是代码“我在职位”。

这是因为我们没有宣布呼叫函数“if__name__==“主”。

如果你使用它:

def main():
    print("I am in the function")

if __name__ == "__main__":
    main()

print("I am out of the function")

产量相当于

I am in the function
I am out of the function

在Python中,如果__name__==“__main__”允许您运行Python文件作为可重复使用的模块或单独的程序。

当Python 翻译器阅读源文件时,它将执行所有源文件中的代码;当Python 作为主程序运行“源文件”时,它设置了特殊变量 __name__ 以具有“__main__”值。

当您执行主函数时,它将读取如果声明,检查 __name__ 是否相当于 __main__。

什么是 __name__?

哪里

>>> print(__name__)
__main__
>>>

此分類上一篇:file.py:

print(__name__)

结果在 __main__

主持人.py:

def somefunction():
    print(__name__)

此分類上一篇:file.py:

import somefile
somefile.somefunction()

结果在Somefile

请注意,当在包或模块中使用时, __name__ 取出文件的名称. 实际模块或包路径的路径不被提供,但有自己的 DunderAlias __file__,这允许这样做。

>>> __name__ = 'Horrify' # Change default from __main__
>>> if __name__ == 'Horrify': print(__name__)
...
>>> else: print('Not Horrify')
...
Horrify
>>>

现在回答如果 __name__ ==“__main__”:

如果是包含代码块的流量控制声明,则将执行,如果所提供的值是真实的,我们已经看到 __name__ 可以采取 __main__ 或它已从中进口的文件名。

这告诉我们,如果运行文件是主要文件(或您正在直接从翻译器运行),那么该条件必须执行。

模块

变量

也可以用 __name__ 做其他,不太常见但有用的事情,我会在这里展示一些:

if __name__ != '__main__':
    # Do some useful things 

运行一个条件,如果文件是主一个和另一个,如果它不是

if __name__ == '__main__':
    # Execute something
else:
    # Do some useful things