storing references instead of copies in a dictionary

bgeddy bgeddy at home.havin.a.break
Thu Jul 17 20:16:17 EDT 2008


castironpi wrote:
> 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'
> 
My "old fashioned" programing paradigms think of this in terms of 
"pointers", a throw back to my schooling in 'C'. I find this general 
form of problem to be common across languages and in some ways hard to 
express in python. The whole idea of labels bound to objects is quite 
alien to traditional terminology. I find one of the main attractions of 
python is this new mindset that the language makes you adopt - a 
different set of tools are at hand for the old school programmer.

castironpi - please give an example of what you are thinking as I find 
this interesting. preferably post some brief example code.



More information about the Python-list mailing list