This article mainly introduces to you the relevant information about the built-in constants in Python. The introduction in the article is very detailed and has certain reference and learning value for everyone. Friends who need it Let’s take a look together below.
Preface
We all know that Python does not have many built-in constants, only 6, namely True, False, None, NotImplemented, Ellipsis, debug. Let’s take a look at the detailed introduction:
1. True
1. True is a bool type used to represent true values constant.
>>> True True >>> type(True) <class 'bool'>
2. Any assignment operation to the constant True will throw a syntax error.
>>> True = 1 SyntaxError: can't assign to keyword
2. False
1. False is a constant of type bool used to represent false values.
>>> False False >>> type(False) <class 'bool'>
2. Any assignment to the constant False will throw a syntax error.
>>> False = 0 SyntaxError: can't assign to keyword
3. None
1. None means none, which is the only value of NoneType.
>>> None #表示无,没有内容输出 >>> type(None) <class 'NoneType'>
2. Any assignment operation to the constant None will throw a syntax error.
>>> None = 2 SyntaxError: can't assign to keyword
3. For function, if there is no return statement, it is equivalent to returning None.
>>> def sayHello(): #定义函数 print('Hello') >>> sayHello() Hello >>> result = sayHello() Hello >>> result >>> type(result) <class 'NoneType'>
4. NotImplemented
1. NotImplemented is a constant of type NotImplementedType.
>>> NotImplemented NotImplemented >>> type(NotImplemented) <class 'NotImplementedType'>
2. Use the bool() function to test and you can find that NotImplemented is a true value.
>>> bool(NotImplemented) True
3. NotImplemented is not a constant in the absolute sense, because it can be assigned without throwing a syntax error, and we should not assign it, otherwise it will affect the execution results of the program.
>>> bool(NotImplemented) True >>> NotImplemented = False >>> >>> bool(NotImplemented) False
4. NotImplemented is mostly used as the return value in some binary special methods (such as eq, lt, etc.), indicating that the method is not implemented, and Python will smartly exchange the two parameters when the result returns NotImplemented. Another try.
>>> 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
When executing a1==a2 (that is, calling eq(a1,a2)) and returning NotImplemented, Python will automatically exchange parameters and call eq(a2,a1) again.
5. Ellipsis
1. Ellipsis is a constant of ellipsis type, which is equivalent to...
>>> Ellipsis Ellipsis >>> type(Ellipsis) <class 'ellipsis'> >>> ... Ellipsis >>> ... == Ellipsis True
2. Use the bool() function to test and you can find that Ellipsis is a true value.
>>> bool(Ellipsis) True
3. Ellipsis is not a constant in the absolute sense, because it can be assigned a value without throwing a syntax error. We should not assign a value to it, otherwise it will affect the execution results of the program.
>>> bool(Ellipsis) True >>> Ellipsis = False >>> bool(Ellipsis) False
4. Ellipsis is mostly used to represent loop data structures.
>>> 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, [...]] >>>
6. debug
1. debug is a bool type constant.
>>> debug True >>> type(debug) <class 'bool'>
2. Any assignment operation to constant debug will throw a syntax error.
>>> debug = False SyntaxError: assignment to keyword
3. If Python is not started with the -O option, this constant is true, otherwise it is false.
Summarize
The above is the detailed content of In-depth understanding of built-in constants in Python. For more information, please follow other related articles on the PHP Chinese website!