【Python】【基础知识】【内置常量】

Python的内置常量有:

False、True、None、NotImplemented、Ellipsis、__debug__

由 site 模块添加的常量:quit、exit、copyright、credits、license

内置常量

有少数的常量存在于内置命名空间中。 它们是:

False

bool 类型的假值。 给 False 赋值是非法的并会引发 SyntaxError

True

bool 类型的真值。 给 True 赋值是非法的并会引发 SyntaxError

None

NoneType 类型的唯一值。 None 经常用于表示缺少值,当因为默认参数未传递给函数时。 给 None 赋值是非法的并会引发 SyntaxError

NotImplemented

二进制特殊方法应返回的特殊值(例如,__eq__()__lt__()__add __()__rsub__() 等)表示操作没有针对其他类型实现;为了相同的目的,可以通过就地二进制特殊方法(例如,__imul __()__ rightnd__()等)返回。 它的逻辑值为真。

注解

 

当二进制(或就地)方法返回``NotImplemented``时,解释器将尝试对另一种类型(或其他一些回滚操作,取决于运算符)的反射操作。 如果所有尝试都返回``NotImplemented``,则解释器将引发适当的异常。 错误返回的``NotImplemented``将导致误导性错误消息或返回到Python代码中的``NotImplemented``值。

参见 实现算数运算 为例。

注解

 

NotImplementedError 和 NotImplemented 不可互换,即使它们有相似的名称和用途。 有关何时使用它的详细信息,请参阅 NotImplementedError

Ellipsis

与省略号文字字面 “...” 相同。 特殊值主要与用户定义的容器数据类型的扩展切片语法结合使用。

__debug__

如果 Python 没有以 -O 选项启动,则此常量为真值。 另请参见 assert 语句。

注解

 

变量名 NoneFalseTrue 和 __ debug__ 无法重新赋值(赋值给它们,即使是属性名,将引发SyntaxError ),所以它们可以被认为是“真正的”常数。

由 site 模块添加的常量

site 模块(在启动期间自动导入,除非给出 -S 命令行选项)将几个常量添加到内置命名空间。 它们对交互式解释器 shell 很有用,并且不应在程序中使用。

quit (code=None)
exit (code=None)

当打印此对象时,会打印出一条消息,例如“Use quit() or Ctrl-D (i.e. EOF) to exit”,当调用此对象时,将使用指定的退出代码来引发 SystemExit

credits

打印或调用的对象分别打印版权或作者的文本。

license

当打印此对象时,会打印出一条消息“Type license() to see the full license text”,当调用此对象时,将以分页形式显示完整的许可证文本(每次显示一屏)。


————————(我是分割线)————————

一般的内置常量

False、True、None、NotImplemented、Ellipsis、__debug__

False、True 属于bool类型

None 属于NoneType 类型

NotImplemented  二进制特殊方法应返回的特殊值

Ellipsis  与省略号文字字面 “...” 相同。

__debug__

>>> __debug__
True

如果 Python 没有以 -O 选项启动,则此常量为真值。 另请参见 assert 语句。


————————(我是分割线)————————

由 site 模块添加的常量

quit()

(在Windows环境执行,直接退出PythonIDLE,不过退出之前会弹出确认对话框)

 

 exit()

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> i = 1
>>> print(i)
1
>>> exit
Use exit() or Ctrl-Z plus Return to exit
>>> exit()

执行后,弹出确认对话框;

copyright

 版权/著作权显示:

>>> copyright
Copyright (c) 2001-2018 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
>>> 

credits

信誉

>>> credits
    Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
    for supporting Python development.  See www.python.org for more information.
>>> 

感谢 CWI, CNRI, BeOpen.com, Zope Corporation 和数以千计的支持python开发的人员。有关详细信息,请参见www.python.org。

license()

 

>>> license()
A. HISTORY OF THE SOFTWARE
==========================

Python was created in the early 1990s by Guido van Rossum at Stichting
Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands
as a successor of a language called ABC.  Guido remains Python's
principal author, although it includes many contributions from others.

In 1995, Guido continued his work on Python at the Corporation for National Research Initiatives (CNRI, see http://www.cnri.reston.va.us) in Reston, Virginia where he released several versions of the software. In May 2000, Guido and the Python core development team moved to BeOpen.com to form the BeOpen PythonLabs team. In October of the same year, the PythonLabs team moved to Digital Creations, which became Zope Corporation. In 2001, the Python Software Foundation (PSF, see https://www.python.org/psf/) was formed, a non-profit organization created specifically to own Python-related Intellectual Property. Zope Corporation was a sponsoring member of the PSF. All Python releases are Open Source (see http://www.opensource.org for the Open Source Definition). Historically, most, but not all, Python Hit Return for more, or q (and Return) to quit: quit Hit Return for more, or q (and Return) to quit: q >>> 


————————(我是分割线)————————

参考:

1. RUNOOB.COM:https://docs.python.org/zh-cn/3/library/constants.html

备注:

初次编辑时间:2019年9月30日17:55:21

环境:Windows 7   / Python 3.7.2

猜你喜欢

转载自www.cnblogs.com/kaixin2018/p/11613737.html