"once" assigment in Python

Lorenzo Di Gregorio lorenzo.digregorio at gmail.com
Mon Sep 17 19:10:20 EDT 2007


On 17 Sep., 16:54, Larry Bates <larry.ba... at websafe.com> wrote:
>
> IMHO variables like what you describe are really data not program variables.
> You might consider putting variables like these in a dictionary and then check
> to see if the keys exist before assignment:
>
> var_dict={}
>
> #
> # See if 'varname' initialized, if not it needs to be
> #
> if 'varname' not in var_dict:
>       var_dict[varname]=somevalue

This is a good point!

I could write something like:

instantiate('component',function)

and have instantiate() to conditionally do:

instance['component']=Module(function)

Of course this means using:

instance['component'].method()

instead of just:

component.method()

A more annoying difficulty is with the fact that, while the scope of
ordinary variables is limited (function, package), the scope of
'component' would be global (the whole instance[]): having global
names could be pretty annoying in modeling a hierarchy.  Anyway, some
scoping mechanism could be implemented over the global dictionary and
this could be a good price to pay to avoid other tricks.
Further suggestions?

Thank you!
Lorenzo




More information about the Python-list mailing list