"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:
> # See if 'varname' initialized, if not it needs to be
> if 'varname' not in var_dict:
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.
More information about the Python-list