"once" assigment in Python

Lorenzo Di Gregorio lorenzo.digregorio at gmail.com
Tue Sep 18 01:10:20 CEST 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:


and have instantiate() to conditionally do:


Of course this means using:


instead of just:


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!

More information about the Python-list mailing list