View Full Version : A bug in python?
Xavieran
October 30th, 2008, 05:09 AM
I'm running python 2.5.2 and I think I may have found a bug...
When I run:
isinstance(True,int)
python returns True...
it also returns true when I substitute False for True...
Is this a bug?
Or was this intended?
Delever
October 30th, 2008, 05:24 AM
The Boolean type is a subclass of the int class so that arithmetic using a Boolean still works.
Xavieran
October 30th, 2008, 05:50 AM
Seems a bit strange though, In my app I need to distuinguish between int and bool...
def opposite(x):
'''Return the opposite of x.'''
if x:
if isinstance(x,bool):return False
elif isinstance(x,int):return 0
else:
if isinstance(x,bool):return True
elif isinstance(x,int):return 1
I've switched the order of ifs so it works correctly...
But it seems rather strange...:confused:
geirha
October 30th, 2008, 06:32 AM
I don't understand why you have to distinguish between bool and int. They are both ints ...
>>> True+True
2
ghostdog74
October 30th, 2008, 07:05 AM
I'm running python 2.5.2 and I think I may have found a bug...
When I run:
isinstance(True,int)
python returns True...
it also returns true when I substitute False for True...
Is this a bug?
Or was this intended?
True has value of 1, False is 0. So what is wrong that you think its wrong?
slavik
October 30th, 2008, 08:47 AM
err, shouldn't true+true be invalid in Python since it isn't duck typed?
Delever
October 30th, 2008, 08:54 AM
err, shouldn't true+true be invalid in Python since it isn't duck typed?
This is good read if you are interested:
http://www.python.org/dev/peps/pep-0285/
loell
October 30th, 2008, 08:56 AM
maybe use isnum? or sumpin equivalent.
slavik
October 30th, 2008, 08:58 AM
This is good read if you are interested:
http://www.python.org/dev/peps/pep-0285/
thanks for the link ... it clears up the decision up a bit ...
loell
October 30th, 2008, 09:03 AM
maybe use isnum? or sumpin equivalent.
def isnum(x) :
# """Return true if x is a number."""
try :
return complex(x) == x
except Exception :
return False
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.