深入理解Python中的内置常量

作者:十月狐狸 时间:2023-01-21 02:57:47 

前言

大家都知道Python内置的常量不多,只有6个,分别是True、False、None、NotImplemented、Ellipsis、__debug__。下面就来看看详细的介绍:

一. True

1. True是bool类型用来表示真值的常量。


>>> True
True
>>> type(True)
<class 'bool'>

2. 对常量True进行任何赋值操作都会抛出语法错误。


>>> True = 1
SyntaxError: can't assign to keyword

二. False

1. False是bool类型用来表示假值的常量。


>>> False
False
>>> type(False)
<class 'bool'>

2. 对常量False进行任何赋值操作都会抛出语法错误。


>>> False = 0
SyntaxError: can't assign to keyword

三. None

1. None表示无,它是NoneType的唯一值。


>>> None #表示无,没有内容输出
>>> type(None)
<class 'NoneType'>

2. 对常量None进行任何赋值操作都会抛出语法错误。


>>> None = 2
SyntaxError: can't assign to keyword

3. 对于函数,如果没有return语句,即相当于返回None。


>>> def sayHello(): #定义函数
print('Hello')

>>> sayHello()
Hello
>>> result = sayHello()
Hello
>>> result
>>> type(result)
<class 'NoneType'>

四. NotImplemented

1.  NotImplemented是NotImplementedType类型的常量。


>>> NotImplemented
NotImplemented
>>> type(NotImplemented)
<class 'NotImplementedType'>

2. 使用bool()函数进行测试可以发现,NotImplemented是一个真值。


>>> bool(NotImplemented)
True

3. NotImplemented不是一个绝对意义上的常量,因为他可以被赋值却不会抛出语法错误,我们也不应该去对其赋值,否则会影响程序的执行结果。


>>> bool(NotImplemented)
True
>>> NotImplemented = False
>>>
>>> bool(NotImplemented)
False

4. NotImplemented多用于一些二元特殊方法(比如__eq__、__lt__等)中做为返回值,表明没有实现方法,而Python在结果返回NotImplemented时会聪明的交换二个参数进行另外的尝试。


>>> class A(object):
def __init__(self,name,value):
 self.name = name
 self.value = value
def __eq__(self,other):
 print('self:',self.name,self.value)
 print('other:',other.name,other.value)
 return self.value == other.value #判断2个对象的value值是否相等

>>> a1 = A('Tom',1)
>>> a2 = A('Jay',1)
>>> a1 == a2
self: Tom 1
other: Jay 1
True

>>> class A(object):
def __init__(self,name,value):
 self.name = name
 self.value = value
def __eq__(self,other):
 print('self:',self.name,self.value)
 print('other:',other.name,other.value)
 return NotImplemented

>>> a1 = A('Tom',1)
>>> a2 = A('Jay',1)
>>> a1 == a2
self: Tom 1
other: Jay 1
self: Jay 1
other: Tom 1
False

当执行a1==a2(即调用__eq__(a1,a2)),返回NotImplemented时,Python会自动交换参数再次调用__eq__(a2,a1)。

五. Ellipsis

1. Ellipsis是ellipsis类型的常量,它和…是等价的。


>>> Ellipsis
Ellipsis
>>> type(Ellipsis)
<class 'ellipsis'>
>>> ...
Ellipsis
>>> ... == Ellipsis
True

2. 使用bool()函数进行测试可以发现,Ellipsis是一个真值。


>>> bool(Ellipsis)
True

3. Ellipsis不是一个绝对意义上的常量,因为他可以被赋值却不会抛出语法错误,我们也不应该去对其赋值,否则会影响程序的执行结果。


>>> bool(Ellipsis)
True
>>> Ellipsis = False
>>> bool(Ellipsis)
False

4. Ellipsis多用于表示循环的数据结构。


>>> a = [1,2,3,4]
>>> a.append(a)
>>> a
[1, 2, 3, 4, [...]]
>>> a
[1, 2, 3, 4, [...]]
>>> len(a)
>>> a[4]
[1, 2, 3, 4, [...]]
>>>

六. __debug__

1. __debug__是一个bool类型的常量。


>>> __debug__
True
>>> type(__debug__)
<class 'bool'>

2. 对常量__debug__进行任何赋值操作都会抛出语法错误。


>>> __debug__ = False
SyntaxError: assignment to keyword

3. 如果Python没有使用-O选项启动,此常量是真值,否则是假值。

总结

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

来源:http://www.cnblogs.com/sesshoumaru/p/python-constants.html

标签:python,内置常量
0
投稿

猜你喜欢

  • python爬虫实战之制作属于自己的一个IP代理模块

    2021-12-23 21:42:20
  • webpack下实现动态引入文件方法

    2024-06-08 16:28:49
  • 解决thinkPHP 5 nginx 部署时,只跳转首页的问题

    2024-05-11 09:22:31
  • Python print函数:如何将对象打印输出

    2023-03-26 23:07:16
  • asp日期时间格式化函数

    2009-12-14 12:56:00
  • python基于exchange函数发送邮件过程详解

    2021-03-23 02:12:22
  • ASP实现长文章自动分页的函数代码

    2008-10-10 17:09:00
  • 基于Python 装饰器装饰类中的方法实例

    2023-10-11 04:10:22
  • 在Python中实现字典反转案例

    2022-12-17 02:05:46
  • js查找父节点的简单方法

    2023-09-11 01:12:42
  • python中利用xml.dom模块解析xml的方法教程

    2022-08-17 01:44:47
  • Golang学习笔记之延迟函数(defer)的使用小结

    2024-02-03 07:56:04
  • python super函数使用方法详解

    2022-04-15 21:45:17
  • python numpy生成等差数列、等比数列的实例

    2023-04-16 14:43:58
  • Go语言中new()和 make()的区别详解

    2024-05-22 17:45:33
  • Mysql数据库之Binlog日志使用总结(必看篇)

    2024-01-20 20:15:16
  • Django实现文件上传和下载功能

    2022-04-02 15:19:34
  • PyCharm2019.3永久激活破解详细图文教程,亲测可用(不定期更新)

    2022-04-27 23:51:16
  • 使用Python搭建虚拟环境的配置方法

    2021-09-04 17:50:58
  • W3C优质网页小贴士(一)

    2008-04-06 16:35:00
  • asp之家 网络编程 m.aspxhome.com