这里有新鲜出炉的 Python3 官方中文指南,程序狗速度看过来!
Python 是一种面向对象、解释型计算机程序设计语言,由 Guido van Rossum 于 1989 年底发明,第一个公开发行版发行于 1991 年。Python 语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是 C/C++)很轻松地联结在一起。
这篇文章主要跟大家介绍了关于 Python 中内置常量的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看吧。
前言
大家都知道 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 选项启动,此常量是真值,否则是假值。
总结
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对 PHPERZ 的支持。
来源: http://www.phperz.com/article/17/0603/335758.html