along the lines, hash and obj. id.

castironpi at gmail.com castironpi at gmail.com
Tue Feb 26 02:58:52 EST 2008


On Feb 25, 11:30 pm, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Mon, 25 Feb 2008 17:55:18 -0800 (PST), castiro... at gmail.com declaimed
> the following in comp.lang.python:
>
> > I'd like to do this:
>
> > a= list( range( 5 ) )
> > assert a== [ 0, 1, 2, 3, 4 ]
> > for i in ref( a ):
> >     i.ref*= 2
> > a= deref( a )
> > assert a== [ 0, 2, 4, 6, 8 ]

[snip]

> > Dictionaries, sets, and the primitives all have ways to do this, in
> > particular, to reset them: a= {} is equivalent to a.clear(), except

<em>EXCEPT THAT</em>

> > that other references to it corefer before, and in the former, don't
> > after.

No, ={} and .clear() are not equivalent.

In the case of class instances,

>>> class Instance(): pass
...
>>> obj= Instance()
>>> obj.a= {}
>>> d= obj.a
>>> obj.a[0]= object()
>>> d[0]
<object object at 0x00A37468>
>>> e= obj
>>> obj.a= {}
>>> d
{0: <object object at 0x00A37468>}
>>> e.a
{}

It works to keep a reference to the object, then access the member
again.  If your only reference to the object is the member itself,
obj.a= {} breaks d, but obj.a.clear() keeps it alive.

In the case of immutables, there is no such .clear() equivalent.  If
obj.a is a tuple, string, or integer (*miss any?), d is always broken
when obj.a is changed.

The generic solution involves a second level of indirection: tuple* d=
&obj.a.  I can't land a clean solution to it though.



More information about the Python-list mailing list