Python Forum
gotcha - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: gotcha (/thread-2131.html)



gotcha - Skaperen - Feb-21-2017

Python Gotchas is closed, so i can't add this there ...

isinstance(False,int) -> True

... now i need to go reorder a bunch of my tests.


RE: gotcha - Larz60+ - Feb-21-2017

Obviously one would think the answer should be False.
In C, True, False, and bool are all defined in stdbool.h,
True as 1, and False as 0 which are both integers.

Which means that technically bool is int, even though by definition is is not.

And python, being written in C, probably (right or wrong) follows suit.


RE: gotcha - zivoni - Feb-21-2017

According to python docs, there are two types of integers - Integers (int) and Booleans (bool), and bool is subclass of int.

Quote:Booleans (bool)

    These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.



RE: gotcha - micseydel - Feb-21-2017

I can see how this is somewhat surprising but it does seem consistent
Output:
>>> True + True + False + True 3



RE: gotcha - Ofnuts - Feb-21-2017

(Feb-21-2017, 04:50 PM)micseydel Wrote: I can see how this is somewhat surprising but it does seem consistent
Output:
>>> True + True + False + True 3

There is also the alternative to x=value_if_true if some_boolean else value_if_false: x=[value_if_false,value_if_true][some_boolean]


RE: gotcha - micseydel - Feb-21-2017

That's a good point too. (I do want to point out that, although the point about being able to use a boolean as an int is valid, the two expressions aren't quite the same given the difference in behavior if there are side effects and performance if the unused expression within the list is expensive to compute.)


gotcha 2.0 - Skaperen - Feb-22-2017

gotcha 2.0

so i update my tests and test for False and/or True like this:

if value == False:
    handle_false()
elif value == True:
    handle_true()
so what if value is 0 or 1?  oops!

i guess i need to do:

if isinstance(value,bool):
    if value == False:
        handle_false()
    elif value == True:
        handle_true()
i bet it could be argued:  it's a feature


RE: gotcha - ichabod801 - Feb-22-2017

Or you want to check against actual boolean values, use is:

if value is False:
    handle_false()
elif value is True:
    handle_true()
True and False are singletons, like None. I don't know why everyone finds bools as ints so surprising. Did no one take discrete math?


RE: gotcha - micseydel - Feb-22-2017

(Feb-22-2017, 02:34 AM)ichabod801 Wrote: I don't know why everyone finds bools as ints so surprising. Did no one take discrete math?
I took discrete math twice, thank you very much. Still don't understand any of it.

Really though I can see an argument being made either way. I'm used to in the statically typed languages I used booleans not being numeric (Scala / Java). As for the other side...

(Feb-22-2017, 02:28 AM)Skaperen Wrote: i bet it could be argued:  it's a feature
Yes! Python is, by convention, duck typed. It's not just dynamically typed and you're not prohibited by the language from type-checking. If it walks like a duck and quacks like a duck, why do you care if it's really a duck or not? I could design an API that does care, but such designs are not considered Pythonic. Much like "it's better to ask forgiveness than permission" being preferable "look before you leap" - there are situations when either is preferable, the culture of the language is to prefer one, and go to the other only in extraordinary circumstances.


RE: gotcha - Skaperen - Feb-22-2017

(Feb-22-2017, 02:34 AM)ichabod801 Wrote: Or you want to check against actual boolean values, use is:

if value is False:
    handle_false()
elif value is True:
    handle_true()
True and False are singletons, like None. I don't know why everyone finds bools as ints so surprising. Did no one take discrete math?

booleans implemented as ints is what i learned (way way back in my mainframe assembler days)  it was so in c because things must be implemented somehow or they don't exist (for what is implemented in c).  that and in the language i designed ages ago booleans were not ints but could be coerced to become an int or even a float.  i need to get more used to python not being the same Pray   that and i need to get used to other python features and subtle differences.  i'm still in learning mode.  i will be for years even if i do big projects in python.

Output:
>>> 0 == False True >>> 0 is False False >>>
i already knew i could use is, but i had a misunderstanding of it being the same as =.  thanks!