Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
gotcha
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Reply
#3
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.
Reply
#4
I can see how this is somewhat surprising but it does seem consistent
Output:
>>> True + True + False + True 3
Reply
#5
(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]
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#6
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.)
Reply
#7
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
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(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.
Reply
#10
(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!
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020