It's weird to compare integers with is

Asked 2 years ago, Updated 2 years ago, 109 views

When running Python 2.5.2

>>> a = 256
>>> b = 256
>>> a is b
True # Of course it's true
>>> 257 is 257
True # If you compare 257 directly, it's true
>>> a = 257
>>> b = 257
>>> a is b
False # Why does it say False?

It runs together.

I tested it to see if it depends on the interpreter version, but in Python 2.3.3, Only 99 to 100 people get this result.

Is Python different in terms of storing small integers and large numbers? I don't know why they're producing such different results.

So how do I compare the two numbers stably?

python int identity operator compare

2022-09-22 22:24

1 Answers

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.