Introduction
Python has a few built-in constants, only 6, True, False, None, notimplemented, ellipsis, __debug__
One. True
1.True is the truth constant used by the bool type to represent
>>> truetrue>>> Type (True) <class ' bool ' >
2. Any assignment to the constant true will throw a syntax error
>>> True = 1 File "<stdin>", line 1syntaxerror:can ' t assign to keyword
Two. False
1.False is a constant of type bool that represents false values
>>> falsefalse>>> Type (False) <class ' bool ' >
2. Any assignment to the constant false will throw a syntax error
>>> False = 0 File "<stdin>", line 1syntaxerror:can ' t assign to keyword
Three. None
1.None means no, he's the only value of Nonetype.
>>> none>>> Type (None) <class ' Nonetype ' >
2. Any assignment to the constant none will throw a syntax error
>>> None = 1 File "<stdin>", line 1syntaxerror:can ' t assign to keyword
3. For a function, if there is no return statement, it is equivalent to returning none
>>> def sayhello (): #定义函数 ...
Four. notimplemented
1.NotImplemented is a constant of type Notimplementedtype
>>> notimplementednotimplemented>>> Type (notimplemented) <class ' Notimplementedtype ' >
2. Testing with the bool () function reveals that notimplemented is a true truth
>>> bool (notimplemented) True
3.NotImplemented is not a constant in absolute sense, because he can be assigned without throwing a syntax error, and we should not assign it, or it will affect the execution of the program.
>>> bool (notimplemented) true>>> notimplemented = False
4.NotImplemented is used for some binary special methods (such as: __eq__,__it__, etc.) as the return value, indicating that there is no implementation method, and Python will be smart when the results return notimplemented two parameters for another attempt
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 NotImplementeda1 = A (' Tome ', 1) a2 = A (' Jim ', 1) print (a1 = = A2) #输出 # self:tome # other:jim # self:jim # other:to Me # False
When you execute a1 = = A2 (that is, call __eq__ (A1,A2)), when you return to notimplemented, Python automatically switches the parameters to call __eq__ again (A2,A1)
Five. Ellipsis
1.Ellipsis is a constant of type ellipsistype, it and ... is equivalent to
>>> ellipsisellipsis>>> Type (ellipsis) <class ' ellipsis ' >>>> ... ellipsis>>>. = = Ellipsistrue
2. Testing with the bool () function reveals that ellipsis is a true truth
>>> bool (ellipsis) True
3. Ellipsis is not a constant in absolute sense, because he can be assigned but does not throw a grammatical error, and we should not assign a value to it, otherwise it will affect the execution result of the program.
>>> bool (ellipsis) true>>> ellipsis = false>>> bool (ellipsis) False
4.Ellipsis multiple data structures to represent loops
>>> 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, [...]
Six. __debug__
1.__DEBUG__ is a constant of type bool
>>> __debug__true>>> Type (__debug__) <class ' bool ' >
2. Any assignment to the constant __debug__ will throw a syntax error
>>> __debug__ = False File "<stdin>", line 1syntaxerror:assignment to keyword
3. If Python is not started with the-o option, this constant is true, otherwise it is a false value.
"Python" python-built-in constants