Lorenzo Di Gregorio wrote: > Hello, > > I've been using Python for some DES simulations because we don't need > full C speed and it's so much faster for writing models. During > coding I find it handy to assign a variable *unless it has been > already assigned*: I've found that this is often referred to as "once" > assigment. > > The best I could come up with in Python is: > > try: > variable > except NameError: > variable = method() > > I wonder if sombody has a solution (trick, whatever ...) which looks > more compact in coding. Something like: > > once(variable, method) > > doesn't work, but it would be perfect. Of course I can preprocess the > Python code but an all-Python solution would be more handy. > > Any suggestions? > > Thx in advance, > Lorenzo > 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 -Larry -- http://mail.python.org/mailman/listinfo/python-list