Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hi all help about boolean
#4
Why would this ever return false?
a =500
a is 500
"a" references the int object 500. This may not be the only 500 int object, but it is the same one created by "a = 500".
a = 500
print(id(a), id(500))
Output:
2003347601776 2003347601776
I think you were trying to test this:
a = 500
print("500 is not always 500", a is int(str(a)))
b = 255
print("255 is always 255", b is int(str(b)))
Output:
500 is not always 500 False 255 is always 255 True
CPython creates int objects for 0 through 256. These are used whenever an int object is needed in this range instead of creating new int objects. But this is an implementation detail and should not be used in your programs. Limit using "is" to testing if two variables reference the same object. Use "==" to test if two objects have the same value.
Reply


Messages In This Thread
hi all help about boolean - by akaw3 - Apr-30-2022, 11:28 AM
RE: hi all help about boolean - by Larz60+ - Apr-30-2022, 12:17 PM
RE: hi all help about boolean - by akaw3 - Apr-30-2022, 01:02 PM
RE: hi all help about boolean - by deanhystad - Apr-30-2022, 07:55 PM

Forum Jump:

User Panel Messages

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