F
Franck Ditter
Hi !
a is b <==> id(a) == id(b) in builtin classes.
Is that true ?
Thanks,
franck
a is b <==> id(a) == id(b) in builtin classes.
Is that true ?
Thanks,
franck
Hi !
a is b <==> id(a) == id(b) in builtin classes.
Is that true ?
Thanks,
franck
Hi !
a is b <==> id(a) == id(b) in builtin classes. Is that true ?
3074285228La = ["some", "object"]
id(a) 3074285228L
del a
b = [100, 200]
id(b)
2a = ["some", "object"]
id(a) 1
del a
b = [100, 200]
id(b)
44a = ["some", "object"]
id(a) 43
del a
b = [100, 200]
id(b)
Not just for builtin classes, for any objects, provided that they are
alive at the same time.
There is no guarantee whether IDs will be re-used. Some versions of
Python do re-use IDs, e.g. CPython:
steve@runes:~$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
3074285228La = ["some", "object"]
id(a)
del a
b = [100, 200]
id(b)
3074285228L
but others do not, e.g. Jython and IronPython:
steve@runes:~$ jython
Jython 2.5.1+ (Release_2_5_1, Aug 4 2010, 07:18:19)
[OpenJDK Client VM (Sun Microsystems Inc.)] on java1.6.0_18
Type "help", "copyright", "credits" or "license" for more information.
2
steve@runes:~$ ipy
IronPython 2.6 Beta 2 DEBUG (2.6.0.20) on .NET 2.0.50727.1433
Type "help", "copyright", "credits" or "license" for more information.
44
CPython especially has the most complicated behaviour with IDs and object
identity:
False
True
In general, you almost never need to care about IDs and object identity.
The main exception is testing for None, which should always be written as:
if x is None
Seeing this thread, I think the is statment should be removed.
It has a replacement syntax of id(x) == id(y) and "a==True" should be automatically changed into memory comparison.
Thanks to all, but :
- I should have said that I work with Python 3. Does that matter ?
- May I reformulate the queston : "a is b" and "id(a) == id(b)"
both mean : "a et b share the same physical address". Is that True ?
Thanks to all, but :
- I should have said that I work with Python 3. Does that matter ?
- May I reformulate the queston : "a is b" and "id(a) == id(b)"
both mean : "a et b share the same physical address". Is that True ?
Thanks,
Seeing this thread, I think the is statment should be removed. It has a
replacement syntax of id(x) == id(y)
and "a==True" should be automatically changed into memory comparison.
No, id() has nothing to do with physical address. The Python language
does not specify anything about physical addresses. Some
implementations may happen to use physical addresses, others arbitrary
integers. And they may reuse such integers, or not. Up to the
implementation.
True. In principle, some day there might be a version of Python that runs
on some exotic quantum computer where the very concept of "physical
address" is meaningless. Or some sort of peptide or DNA computer, where
the calculations are performed via molecular interactions rather than by
flipping bits in fixed memory locations.
But less exotically, Frank isn't entirely wrong. With current day
computers, it is reasonable to say that any object has exactly one
physical location at any time. In Jython, objects can move around; in
CPython, they can't. But at any moment, any object has a specific
location, and no other object can have that same location. Two objects
cannot both be at the same memory address at the same time.
So, for current day computers at least, it is reasonable to say that
"a is b" implies that a and b are the same object at a single location.
But by claiming that id() really means address, and that those addressesThe second half of the question is more complex:
"id(a) == id(b)" *only* implies that a and b are the same object at the
same location if they exist at the same time. If they don't exist at the
same time, then you can't conclude anything.
On 09/05/2012 10:41 AM, Steven D'Aprano wrote: [...]So, for current day computers at least, it is reasonable to say that "a
is b" implies that a and b are the same object at a single location.
You're arguing against something i didn't say. I only said that id()
doesn't promise to be a memory address. i said nothing about what it
might mean if the "is" operator considers them the same.
But by claiming that id() really means address,
and that those addresses
might move during the lifetime of an object, then the fact that the id()
functions are not called simultaneously implies that one object might
move to where the other one used to be before the "move."
I don't claim to know the jython implementation. But you're claiming
that id() means the address of the object, even in jython.
I think it much more likely that jython uses integer values for the id()
function, and not physical addresses.
But by claiming that id() really means address, and that those addresses
might move during the lifetime of an object, then the fact that the id()
functions are not called simultaneously implies that one object might
move to where the other one used to be before the "move."
I think it much more likely that jython uses integer values for
the id() function, and not physical addresses.
You *cannot* replace is with id() except when the objects are guaranteed
to both be alive at the same time, and even then you *shouldn't* replace
is with id() because that is a pessimation (the opposite of an
optimization -- something that makes code run slower, not faster).
Which is equivalent to my point. I had mistakenly thought that StevenWhoa! Not so fast! The id() of an object is guaranteed to not
change during the object's lifetime. So if an implementation
moves objects around (e.g. Jython), then it cannot use memory
addresses for the id() function.
The id() function is guaranteed to return some flavour of integer.
In Jython, the return values are 1, 2, 3, 4, etc., except, of course,
if you invoke id() on an object you've id'd before, you get the same
number as before.
In current versions of CPython, you do get the (virtual) memory
address, converted to an int (or a long). But then, CPython does
not move objects.
Maybe the next version of CPython should shift id values two or three
bits to the right, just to make sure people don't misinterpret ids as
memory addresses.
Seeing this thread, I think the is statment should be removed.
It has a replacement syntax of id(x) == id(y)
and "a==True" should be automatically changed into memory comparison.
True. In principle, some day there might be a version of Python that runs
on some exotic quantum computer where the very concept of "physical
address" is meaningless.
The thread is wrong then.
If the implementation reuses ids, which CPython does,
<expression-1> is <expression-2>
must be implemented as
internal-tem1 = <expression-1>
internal-tem2 = <expression-2>
id(internal-tem1) == id(internal-tem2)
in order to ensure that the two objects exist simultaneously,
so that the id comparison is valid.
I have no idea what that means.
You mean like the human brain? When people execute Python code, does 0
have an address?
I have no idea what that means.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.