storing references instead of copies in a dictionary

bgeddy bgeddy at home.havin.a.break
Fri Jul 18 02:36:44 CEST 2008


bgeddy wrote:
> 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.

castironpi  - please forgive the double post but my newsreader didn't 
display your code correctly.. Doh !! Anyway - a nice way of addressing 
the problem. However the OP's post revolved around having a rewritable 
set of "labels" - which could be recorded at one time and when re 
referenced the new definitions of those labels would be used. For 
example a "collection" (list,dictionary,tuple) could be made of these 
"labels" and then the underlying code accessed by the labels changed. If 
the code was now ran indirectly by referencing the list then the new 
code would be ran. These building blocks are how parsers are built and 
the basis of language.
I can see how you form two ways of addressing the variable but can't 
figure how this fits the original problem. Please elaborate for my 
ignorance.

EdH.



More information about the Python-list mailing list