In depth understanding of the built in constants in Python
- 2020-06-01 10:13:42
- OfStack
preface
As is known to all, Python has not many built-in constants, but only six, which are True, False, None, NotImplemented, Ellipsis, and s 10en__. Here's a look at the details:
1. True
1. True is a constant of type bool used to represent truth values.
>>> True
True
>>> type(True)
<class 'bool'>
2. Any assignment to the constant True throws 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 throws a syntax error.
>>> False = 0
SyntaxError: can't assign to keyword
3. None
1. None means none; it is the only 1 value of NoneType.
>>> None # Means none, no content output
>>> type(None)
<class 'NoneType'>
2. Any assignment to the constant None throws a syntax error.
>>> None = 2
SyntaxError: can't assign to keyword
3. For functions, if there is no return statement, None is returned.
>>> def sayHello(): # Define a function
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. By using the bool() function, it can be found that NotImplemented is a truth value.
>>> bool(NotImplemented)
True
3. NotImplemented is not a constant in the absolute sense, because it can be assigned without throwing syntax errors, and we should not assign a value to it, otherwise it will affect the result of the program's execution.
>>> bool(NotImplemented)
True
>>> NotImplemented = False
>>>
>>> bool(NotImplemented)
False
4. NotImplemented is mostly used as the return value in some 1/2 yuan special methods (e.g., s/s 76en__, s/s 77en__, etc.), indicating that no method is implemented, while Python will cleverly exchange the 2 parameters and try again when the result returns to NotImplemented.
>>> True = 1
SyntaxError: can't assign to keyword
0
>>> True = 1
SyntaxError: can't assign to keyword
1
When a1==a2(i.e. call s 86en2 (s 87en1, s 88en2)), and return s 89en, Python will automatically exchange the parameters again by calling s 91en__ (s 92en2, s 93en1).
5. Ellipsis
1. Ellipsis is a constant of type ellipsis, which is the same as... It's equivalent.
>>> True = 1
SyntaxError: can't assign to keyword
2
2. Using the bool() function, it can be found that Ellipsis is a truth value.
>>> bool(Ellipsis)
True
3. Ellipsis is not a constant in the absolute sense, because it can be assigned without throwing a syntax error, and we should not assign a value to it, otherwise it will affect the execution result of the program.
>>> True = 1
SyntaxError: can't assign to keyword
4
4. Ellipsis is mostly used to represent the data structure of the loop.
>>> True = 1
SyntaxError: can't assign to keyword
5
6. __debug__
1. S 122en__ is a constant of type bool.
>>> True = 1
SyntaxError: can't assign to keyword
6
2. Any assignment to the constant s s 127en__ will throw a syntax error.
>>> True = 1
SyntaxError: can't assign to keyword
7
3. If Python is not started with the -O option, this constant is true, otherwise it is false.
conclusion