[pypy-dev] Object identity and dict strategies

Bengt Richter bokr at oz.net
Sun Jul 10 18:08:41 CEST 2011


On 07/10/2011 04:09 PM Armin Rigo wrote:
> Hi Bengt,
>
> On Sun, Jul 10, 2011 at 3:48 PM, Bengt Richter<bokr at oz.net>  wrote:
>>   >>>  id([1]) == id([2])
>> True
>
True, I did write that. The key word in my line
before that was `superficially' ;-)

> As pointed out by Carl Friedrich, the real definition of "id" is:
>
> * if x and y are two variables, then "x is y"<=>  "id(x) == id(y)".
>
So conceptually, id is about "variables" -- not objects?
Isn't a "variable" just a special case of an expression
(yielding a reference to a result object, like other expressions)?

ISTM that they key thing above is that "if x and y are two variables"
the two (or one if same) referenced objects are guaranteed
to be live at the same time. So they can't be in the same "location"
unless they are the same object. Someone else mentioned this liveness
issue too.

> That's why in any Python implementation,
>
>>>> x=[1]; y=[2]; id(x) == id(y)
>
> must return False, but not necessarily id([1]) == id([2]).
>
<nit>The definition with "two variables" seems unnecessarily restrictive,
unless I am wrong that x=[1]; id(x) == id([2]) must also return false,
since the x reference guarantees that the [2] cannot be created in the
same "location" as the [1] held by the x.</nit>

Something about returning location from id reminds me of the need for
automatic closure creation in another context.

Letting the expression result die and returning a kind of pointer
to where the result object *was* seems like a dangling pointer problem,
except I guess you can't dereference an id value (without hackery).

Maybe id should raise an exception if the argument referenced only has
a ref count of 1 (i.e., just the reference from the argument list)?

Or else let id be a class and return a minimal instance only binding
the passed object, and customize the compare ops to take into account
type diffs etc.? Then there would be no id values without corresponding
objects, and id values used in expressions would die a natural death,
along with their references to their objects -- whether "variables"
or expressions.

Sorry to belabor the obvious ;-)
Regards,
Bengt

>
> A bientôt,
>
> Armin.




More information about the pypy-dev mailing list