Why does Python override the value of False and True?

4

I'm studying Python and in these studies I ended up with something curious.

> False = True
#Imprime: True

> print(False)
#Imprime: True

> False = bool(0)
> print(False)
#Imprime: False

That is, I was able to rewrite the value of False by giving it the value of True .

In languages such as javascript or PHP it is not possible to do this reassignment, because False is a language constructor or even a constant.

Why can not Python do this? How is False treated in Python ? As a variable?

    
asked by anonymous 03.09.2015 / 22:17

1 answer

4

Why in Python is it possible to do this?

Because they are not reserved words, like in PHP or other languages. Scope variables with defined values (the documentation puts the word "constant", but you can actually have variables with the names True and False ).

How is False treated in Python? As a variable?

False in Python is equivalent to integer 0, and True , to integer 1:

int(False)
=> 0
int(True)
=> 1

Addendum: Not worth for any Python

This applies only to Python 2. For Python 3, the following occurs:

PS C:\Users\lsanches> python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> True = False
  File "<stdin>", line 1
SyntaxError: can't assign to keyword
    
03.09.2015 / 22:21