storing references instead of copies in a dictionary

castironpi castironpi at gmail.com
Thu Jul 17 15:21:15 EDT 2008


On Jul 17, 10:05 am, mk <mrk... at gmail.com> wrote:
> > def f2(arg):
> >     return "f2 "+arg
>
> > def f1(arg):
> >     return "f1 "+arg
>
> > a={"1":"f1","2":"f2"}
> > print [eval(x[1])(x[0]) for x in a.items()]
> > def f2(arg):
> >     return "New f2 "+arg
> > print [eval(x[1])(x[0]) for x in a.items()]
>
> Neat trick, if probably dangerous in some circumstances. Anyway, thanks,
> I didn't think of that.
>
> > Don't know if this is any use to you..
>
> At least I learned something. :-)

You want consistent access to a changing variable.  Wrap it in an
object:

>>> a= Blank( )
>>> a.ref= 'X'
>>> a.ref
'X'
>>> b= a
>>> b.ref
'X'
>>> a.ref= 'Y'
>>> b.ref
'Y'
>>>




More information about the Python-list mailing list