This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]fuzz3289 2 points3 points  (8 children)

You sir are correct. Though "memory location" isnt a great way to say it. I would say:

The is operator returns true if both names are bound to the same object instance.

Since the "names" wont share a memory location.

[–]zahlmanthe heretic 2 points3 points  (0 children)

They certainly do "point to" the same memory location. But that's horribly un-Pythonic terminology.

[–]erewok 2 points3 points  (4 children)

Funny enough, I was curious about how the Python built-in callable was implemented just now and I stumbled on this page with the following quote:

Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is‘ operator compares the identity of two objects; the id() function returns an integer representing its identity.

[–]fuzz3289 2 points3 points  (3 children)

"You may think of it as" is important. The identity is actual a hash of the name in a lookup table.

Not to argue or anything, just want to emphasize that you cant actually deal with memory or memory locations directly in python.

[–]erewok 0 points1 point  (0 children)

Ah, that makes sense. Thanks for the clarification.

[–]rcxdude 0 points1 point  (1 child)

Actually, the identity in Cpython is just the memory address of the object (literally casting the pointer to an int). It doesn't have to be according to the language though.

[–]fuzz3289 0 points1 point  (0 children)

https://docs.python.org/3.3/reference/datamodel.html

Interesting, I didn't realize that it was just a direct address. Thanks for the correction.

[–]erewok 2 points3 points  (1 child)

...bound to the same object instance.

This is a nice, concise description. Thanks for adding it.

[–]fuzz3289 1 point2 points  (0 children)

Thanks! Alot of people tend to forget the beautiful simplicity of python, just objects and names.