We know how bool()
acts on various python objects such as str
, int
, list
.
This is a question about the reverse.
You can cast bool to int as
>>> int(True)
1
>>> int(False)
0
which I think kinda makes sense, but with string we get
>>> str(False)
'False'
>>> str(True)
'True'
which I don't get, as firstly it seems to imply some relation between False
and 'False'
, which only seems relevant at the code level. If what is written in code is to be treated this way, how does this work ...
>>> str(not True)
'False'
Second, it's not obvious it's for consistency, as
>>> bool(str(False))
True
My question is ... is there a reason we're allowed to cast bool
to str
in this way? list
for example won't allow it ...
>>> list()
[]
>>> bool()
False
>>> bool(list())
False
>>> list(bool())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'bool' object is not iterable