Why does `is` return False even though id's are identical?

damian

I already looked at this question, but I don't really get why the two id() calls return an identical value, while the is comparison returns False.

>>> [2,2,2] + [1] == [2,2,2,1]
True
>>> [2,2,2] + [1] is [2,2,2,1]
False
>>> id([2,2,2] + [1])
4396847688
>>> id([2,2,2,1])
4396847688

To be sure, I did expect the two comparisons to return True and False as they did, I just don't get why the ids are not different.

Tim Peters

Just re-entering my comment as "an answer":

Simply because the same memory is reused for the objects: in the last two statements, the lifetimes of the objects whose id's you're obtaining do not overlap, so Python is free to (re)use the same memory for them.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does s1 not equal s3 even though output is identical?

Why does my regression model return an intercept even though I set fit_intercept= False?

Why does torch.cuda.is_available() return false even though I have it installed?

Why does this void function return an integer even though though there is no return statement?

return false even though data is present in the array

Why does my loop not stop, even though it's at the end?

Why does this produce an assertionerror even though it's exactly what was taught?

Why would UndoManager.canRedo() return false even though redo() works fine?

why does JavaScript array.every() method return false when comparing two identical arrays?

Why does Object.Equals() return false for identical anonymous types when they're instantiated from different assemblies?

Why does this return False

Why is std::is_const::value 'false' even though T's value_type is const?

why []==[] and {}=={} return false though [] and {} are truthy values?

Why does std::is_convertible_v return true even though this conversion isn't legal?

Why does querySelectAll return empty NodeList even though there should be three buttons?

Within fakeroot, why does chown return EPERM even though it succeeds (but then again it didn't)?

Why does Nginx return a 403 even though all permissions are set properly?

Why does the Split type only return &str even though Pattern has implementations for both &str and char?

Why does aiohttp.ClientSession() object .get() Method Return Results In Order Even Though It Is Asynchronous?

Component.js: Why does "this.getRouter()" return undefined even though "routing" is configured in manifest.json?

Why does whereis() return undefined even though I just registered the process?

Why does the function return 'undefined', even though the variable contains the correct result?

Why doesn't this POST ever resolve and return a 200 code even though the database does get updated?

Why does False==False in [False] return True?

Does HTML contenteditable attribute require a =true for it to work? Why does it work without it even though it's not a boolean attribute?

It always return FALSE even though every single variable satisfies conditions

$and operator always return true even though i think it should be false

Comparing two lists returns True even though it needs to return False

Why TypeScript excepts a correct return of a overriden method, even though it's not reachable?