Python 中的__main__和__name__

Python 中的__main__和__name__

用 C 族语言(C、C++、Java、C# 等)编写的程序。)需要main()功能来指示执行的起点。

另一方面,在 Python 中,没有main()函数的概念,因为它是一种基于解释器的语言,同样可以在交互 Shell中使用。 扩展名为.py的 Python 程序文件包含多个语句。Python 程序文件的执行从第一条语句开始。

Python 包含名为__name__的特殊变量,该变量包含作为字符串执行的代码的范围。__main__是顶层代码执行的顶层作用域的名称。

例如,解释器 Shell 中执行的代码的范围将是__main__,如下所示。

Python Shell

>>>__name__'__main__'

Copy

所有的功能和模块都将在解释器 Shell 的顶层范围__main___内执行。

Python Shell

>>> def f1():
    print(__name__)>>> f1()

Copy

甚至内部功能都是在顶层范围__main__内执行的:

Python Shell

>>> def f1():
    print(__name__)
    def f2():
        print(__name__)
    f2()>>> f1()__main__
__main__

Copy

一个 Python 文件可以包含多个可以独立执行的函数和语句。例如,考虑以下addition.py:

addition.py

def add(x,y):
    z=x+y    print('add() executed under the scope: ', __name__)
    return z

x=input('Enter the first number to add: ')y=input('Enter the secode number to add: ')result = add(int(x),int(y))print(x, '+', y,'=', result)print('Code executed under the scope: ', __name__)

Copy

Python 程序文件可以通过以下方式执行:

  1. 使用命令提示符/终端将 Python 文件作为脚本执行。

  2. 使用 Import 语句将 Python 代码从一个文件导入到另一个文件

C:\Python37> python addition.py
Enter the first number to add: 3Enter the secode number to add: 3add() executed under the scope: __main__3 + 3 = 6Code executed under the scope: __main__

Copy

可以看到,顶层范围__main__下执行的addition.py

addition.py文件可以作为模块在另一个文件中使用,也可以通过导入在交互 Shell 中使用。

让我们看看当你在交互 Shell 中导入addition模块时会发生什么。

Python Shell

>>> import addition
Enter the first number to add: 3Enter the secode number to add: 3add() executed under the scope:  addition3 + 3 = 6Code executed under the scope:  addition

Copy

上面,导入语句从第一条语句开始执行。但是,我们只想使用add()方法,不想执行其他语句。

这里我们可以使用特殊变量__name__来检查addition.py文件的作用域和执行语句,只有当它从命令提示符/终端独立执行时,而不是当它被导入到其他文件/模块中时。 重写addition.py,如下图。

addition.py

def add(x, y):
    z=x+y    print('add() executed under the scope: ', __name__)
    return zif __name__ == '__main__':
    x=input('Enter the first number to add: ')
    y=input('Enter the secode number to add: ')
    result = add(int(x),int(y))
    print(x, '+', y,'=', result)
    print('Code executed under the scope: ', __name__)

Copy

以上,if 条件检查如果范围是__main__,那么只执行接受用户输入并添加它们的代码。

现在,让我们看看当我们在交互 Shell 中导入上面的addition模块时会发生什么。

Python Shell

>>> import addition>>> addition.add(3,3)add() executed under the scope:  addition6

Copy

也可以使用from import语句,如下所示:

Python Shell

>>> from addition import add>>> add(3,3)add() executed under the scope:  addition6

Copy

如您所见,因为我们使用了一个 if 条件来检查作用域,所以它在导入addition模块后不会执行用户输入的代码,因为它是在模块的作用域下执行的,也就是addition作用域。 只进口add()法。在其他模块中导入addition模块也会发生同样的情况。

现在,让我们看看当您从命令提示符/终端执行它时会发生什么。

C:\Python37> python addition.py
Enter the first number to add: 3Enter the secode number to add: 3add() executed under the scope: __main__3 + 3 = 6Code executed under the scope: __main__

Copy

可以看到,由于addition.py是在顶级范围__main__内执行的,所以还是执行同样的代码。

因此,name的值允许 Python 解释器确定模块是否是可执行脚本。如果其值为main,将执行函数定义之外的语句。如果没有,模块的内容将被填充到顶层模块(或解释器名称空间)中,而不包含可执行部分。

注意:从命令提示符/终端执行的 Python 脚本文件将在顶层作用域__main__作用域下执行。但是,导入模块将在模块自己的范围内执行。因此,顶层范围将是__main__,第二个范围将是模块的范围。

因此,使用特殊变量__name__和顶级范围__main__增加了可重用性。Python 脚本文件可以作为独立脚本从命令提示符/终端执行,也可以作为模块导入。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值