Why is True in Python equal to 1

At the beginning, you need to use the following function to make a judgment, and do some subsequent judgments based on the returned value:

def is_success(param):
	if not param:
		return False
	return True

def process():
	ret = is_sucess('p')
	if ret:
		print 'success'
	else:
		print 'failed'

process()

Later, the requirements were changed, and the results returned in the case of failure should be more detailed, so that more content can be prompted when calling, instead of simply prompting errors, so the above function is changed as follows

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
def is_success(param):
	if param == 0:
		return 0
	elif param == 1:
		return 1
	return True

def process():
	ret = is_sucess('p')
	if ret == 0:
		print 'failed-0'
	elif ret == 1:
		print 'failed-1'
	else:
		print 'success'

process()

When the result was called, the problem came. The expected "success" did not appear, but "failed-1" was printed, that is, the returned True and 1 are equal. Is this true? why?

Executed it in ipython, and found that it is really, as follows

In [1]: True == 1
Out[1]: True

Check the documentation and find that bool is a subclass of int, and there are only two instances of True and False, which explains why True == 1 is true.

class bool(int)
 |  bool(x) -> bool
 |
 |  Returns True when the argument x is true, False otherwise.
 |  The builtins True and False are the only two instances of the class bool.
 |  The class bool is a subclass of the class int, and cannot be subclassed.
 |
 |  Method resolution order:
 |      bool
 |      int
 |      object
 |
 |  Methods defined here:
 |
 |  __and__(...)
 |      x.__and__(y) <==> x&y

Since bool is a subclass of int, True and Fasle can also perform arithmetic operations. After a test, it is true!

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
In [3]: True + 2
Out[3]: 3

In [4]: True + True
Out[4]: 2

In [5]: False * 3
Out[5]: 0

In [6]: 4 / False
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-6-f1cbcd91af4b> in <module>()
----> 1 4 / False

ZeroDivisionError: integer division or modulo by zero

In fact, True and False are built-in variables in Python2. Since they are variables, they can be assigned like other variables. In fact, this is quite tricky, but fortunately, they have been changed to keywords in Python3.

# python2	
In [14]: import __builtin__

In [15]: dir(__builtin__)[41]
Out[15]: 'True'

In [16]: True=233

In [17]: True
Out[17]: 233

In [22]: import keyword

In [23]: keyword.iskeyword(True)
Out[23]: False

In [24]: keyword.iskeyword('True')
Out[24]: False

In Python3, True has been changed to a keyword, so that you can't just assign values ​​and cause some potential problems

>>> import keyword
>>> keyword.iskeyword('True')
True
>>> True=2
  File "<stdin>", line 1
SyntaxError: can't assign to keyword

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/108664579