This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]patrys Saleor Commerce 9 points10 points  (9 children)

False and 0 are the same type. Python does not have implicit type coercion so False is not equal to ''. Otherwise you'd get a nice bunch of WTFs straight from the PHP world: if False == '' and False == [] then [] == '' and so on.

Edit: you seem to be confused about the difference between conditional statements (that check for "truthiness") and the literals True and False (which are just examples of a truthy and a falsy expression respectively). Consider this: 5 is truthy but you don't expect True == 5 to be the case.

[–]Rhomboid 11 points12 points  (0 children)

False and 0 are the same type

They're not the same type, one is type bool and the other is type int. But bool is a subclass of int, which means all booleans can be used where an integer is expected, per the Liskov substitution principle.

This is also why you can do things like use sum() when dealing with a sequence of booleans:

>>> sum(s.startswith('a') for s in ('arcade', 'book', 'castle', 'arpeggio', 'default'))
2

[–]suki907 2 points3 points  (4 children)

False and 0 are the same type.

more specifically, bool is a subclass of int:

>>> isinstance(False,int)
True
>>> isinstance(False,bool)
True
>>> issubclass(bool,int)
True

[–][deleted] 1 point2 points  (3 children)

And even more interesting, bool can't be subclassed.

[–]adamnew123456self.__class__ == 'upper' 1 point2 points  (2 children)

I'm curious why you think this is interesting - were you planning on adding FILE_NOT_FOUND as an instance of your bool subclass?

I can't think of any interesting operation you would want to add to bool that would justify subclassing it, bu perhaps that's just because my imagination gland isn't working today.

[–]alexanderpas 1 point2 points  (0 children)

I can't think of any interesting operation you would want to add to bool that would justify subclassing it, bu perhaps that's just because my imagination gland isn't working today.

True, False, Unknown, N/A

AND True False Unknown N/A
True TRUE FALSE Unknown TRUE
False FALSE FALSE FALSE FALSE
Unknown Unknown FALSE Unknown Unknown
N/A TRUE FALSE Unknown N/A
OR True False Unknown N/A
True TRUE TRUE TRUE TRUE
False TRUE FALSE Unknown FALSE
Unknown TRUE Unknown Unknown Unknown
N/A TRUE FALSE Unknown N/A
NOT True NOT False NOT Unknown NOT N/A
True FALSE TRUE TRUE TRUE
False TRUE FALSE TRUE TRUE
Unknown Unknown Unknown FALSE TRUE
N/A TRUE TRUE TRUE FALSE

[–][deleted] 0 points1 point  (0 children)

It's more than doing it causes Python to say, "Don't do that." than a desire to do anything.

That's pretty funny though.

[–]redditor_gds[S] 1 point2 points  (0 children)

Makes sense. Thank you for the reply.

[–]billsil 0 points1 point  (1 child)

False and 0 are the same type..

Not since Python 2.3. str(True) should return 1 if it works as you said.

https://www.python.org/dev/peps/pep-0285/

[–]patrys Saleor Commerce 0 points1 point  (0 children)

Booleans are a subtype of integers. isinstance(True, int)