[Python-Dev] object equality vs identity, in and dicts idioms and speed

Samuele Pedroni Samuele Pedroni" <pedroni@inf.ethz.ch
Fri, 4 Jan 2002 22:55:14 +0100


Martin explanation is correct.

[Martin v. Loewis]
> You can do it like this:
> 
> map = {}
> 
> def wrap(java.lang.Object o):
>   try:
>     return map[o]
>   except KeyError:
>     map[o] = res = wrapper(o, new_id())
>     return res
> 
> That requires a map lookup every time a wrapper is created; clearly
> undesirable. I think Samuele had something in mind like:
> 

With this approach you could use less memory if there is
much wrapper duplication, but typically a Java object
does not get many long-lived different wrappers.

This "wrap" is quite a core operation, and the map need
to be weak otherwise you leak badly.
 That means that you can implement it only
with java >1.2 and anyway weak-dictionaries in java
require dealing with polling queues of reset weak-refs.
This means complication and slowdown where we
would prefer to avoid it.

regards.