当前位置:Gxlcms > Python > Python中内置常量的深入理解

Python中内置常量的深入理解

时间:2021-07-01 10:21:17 帮助过:58人阅读

这篇文章主要跟大家介绍了关于Python中内置常量的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看吧。

前言

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

一. True

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

  1. >>> True
  2. True
  3. >>> type(True)
  4. <class 'bool'>

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

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

二. False

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

  1. >>> False
  2. False
  3. >>> type(False)
  4. <class 'bool'>

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

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

三. None

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

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

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

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

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

  1. >>> def sayHello(): #定义函数
  2. print('Hello')
  3. >>> sayHello()
  4. Hello
  5. >>> result = sayHello()
  6. Hello
  7. >>> result
  8. >>> type(result)
  9. <class 'NoneType'>

四. NotImplemented

1. NotImplemented是NotImplementedType类型的常量。

  1. >>> NotImplemented
  2. NotImplemented
  3. >>> type(NotImplemented)
  4. <class 'NotImplementedType'>

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

  1. >>> bool(NotImplemented)
  2. True

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

  1. >>> bool(NotImplemented)
  2. True
  3. >>> NotImplemented = False
  4. >>>
  5. >>> bool(NotImplemented)
  6. False

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

  1. >>> class A(object):
  2. def init(self,name,value):
  3. self.name = name
  4. self.value = value
  5. def eq(self,other):
  6. print('self:',self.name,self.value)
  7. print('other:',other.name,other.value)
  8. return self.value == other.value #判断2个对象的value值是否相等
  9. >>> a1 = A('Tom',1)
  10. >>> a2 = A('Jay',1)
  11. >>> a1 == a2
  12. self: Tom 1
  13. other: Jay 1
  14. True
  1. >>> class A(object):
  2. def init(self,name,value):
  3. self.name = name
  4. self.value = value
  5. def eq(self,other):
  6. print('self:',self.name,self.value)
  7. print('other:',other.name,other.value)
  8. return NotImplemented
  9. >>> a1 = A('Tom',1)
  10. >>> a2 = A('Jay',1)
  11. >>> a1 == a2
  12. self: Tom 1
  13. other: Jay 1
  14. self: Jay 1
  15. other: Tom 1
  16. False

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

五. Ellipsis

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

  1. >>> Ellipsis
  2. Ellipsis
  3. >>> type(Ellipsis)
  4. <class 'ellipsis'>
  5. >>> ...
  6. Ellipsis
  7. >>> ... == Ellipsis
  8. True

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

  1. >>> bool(Ellipsis)
  2. True

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

  1. >>> bool(Ellipsis)
  2. True
  3. >>> Ellipsis = False
  4. >>> bool(Ellipsis)
  5. False

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

  1. >>> a = [1,2,3,4]
  2. >>> a.append(a)
  3. >>> a
  4. [1, 2, 3, 4, [...]]
  5. >>> a
  6. [1, 2, 3, 4, [...]]
  7. >>> len(a)
  8. >>> a[4]
  9. [1, 2, 3, 4, [...]]
  10. >>>

六. debug

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

  1. >>> debug
  2. True
  3. >>> type(debug)
  4. <class 'bool'>

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

  1. >>> debug = False
  2. SyntaxError: assignment to keyword

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

总结

以上就是Python中内置常量的深入理解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行