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? > Without being fatuous, I would suggest you rethink your approach to the problem.
Do your variables have default values? If not, what happens if the user does not set them and your code tries to refer to them? You seem to be trying to apply defaults if the user hasn't set a value, when the correct things to do is to apply the defaults *before the user gets a chance to do anything at all*. Then any changes made by the user will overwrite the defaults. Even better, if its practical, is to provide your functionality as one or more functions, whose definitions can set default values for keyword arguments. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://del.icio.us/steve.holden Sorry, the dog ate my .sigline -- http://mail.python.org/mailman/listinfo/python-list