引言

Python的内置常量不多,只有6个,分别是True、False、None、NotImplemented、Ellipsis、__debug__

一.True

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

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

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

>>> True = 1
File "<stdin>", line 1
SyntaxError: can't assign to keyword

二.False

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

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

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

>>> False = 0
File "<stdin>", line 1
SyntaxError: can't assign to keyword

三.None

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

>>> None
>>> type(None)
<class 'NoneType'>

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

>>> None = 1
File "<stdin>", line 1
SyntaxError: can't assign to keyword

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

>>> def sayhello():  #定义函数
... print('hello world')
...
>>>
>>> sayhello()
hello world
>>> ret = sayhello()
hello world
>>> print(ret)
None
>>>

四.NotImplemented

  1.NotImplemented是NotImplementedType类型的常量

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

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

>>> bool(NotImplemented)
True

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

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

  4.NotImplemented多用于对一些二元特殊方法(比如:__eq__,__It__等)中作为返回值,表明没有实现方法,而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 #判断两个对象的value值是否相等
a1 = A('Tome',1)
a2 = A('Jim',1)
print(a1 == a2)
#输出
#self: Tome 1
#other: Jim 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('Tome',1)
a2 = A('Jim',1)
print(a1 == a2)
#输出
# self: Tome 1
# other: Jim 1
# self: Jim 1
# other: Tome 1
# False

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

五.Ellipsis

  1.Ellipsis是EllipsisType类型的常量,它和...是等价的

>>> 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, [...]]
>>> len(a)
5
>>> a[4]
[1, 2, 3, 4, [...]]
>>> a[4][4]
[1, 2, 3, 4, [...]]

六.__debug__

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

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

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

>>> __debug__ = False
File "<stdin>", line 1
SyntaxError: assignment to keyword

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

最新文章

  1. 简单谈谈eclipse下搭建PhoneGap环境来开发Android程序 - linux86(转)
  2. iOS各种调试技巧豪华套餐
  3. 在启动dubbo框架时报错。Unable to connect to zookeeper server within timeout: 5000
  4. MySQL中like的使用方法
  5. RDO部署openstack(2)
  6. Javascript高级程序设计
  7. 从xib加载文件
  8. background-position 个人理解
  9. ASP.NET - 出错页
  10. ZOJ2105 终于找到错误
  11. AtCoder Grand Contest 013
  12. c# 多种方法调整屏幕亮度
  13. 详解java定时任务---Timer篇
  14. python(leetcode)-14最长公共前缀
  15. httpd htpasswd命令
  16. yoj维护
  17. 微信小程序-开心大转盘(圆盘指针)代码分析
  18. CPU 架构 —— ARM 架构
  19. Java虚拟机(JVM)你只要看这一篇就够了!
  20. Python学习---Django的request.post源码分析

热门文章

  1. Promise.race 的原理
  2. ivew 限制输入 0 到 1 的数字 包括小数, 0 ,1
  3. rocketmq启动broker内存占用过大的问题
  4. redis集群报错:(error) CLUSTERDOWN The cluster is down
  5. django2 + python3 显示静态文件中的图片
  6. UIWebView和WKWebView一些琐事
  7. 简单的DOS攻击之死亡之ping详解
  8. CSS入门基础学习二
  9. OUC-NULL -凡事遇则立
  10. 20180708-Java运算符