"once" assigment in Python

Lorenzo Di Gregorio lorenzo.digregorio at gmail.com
Fri Sep 14 08:16:56 CEST 2007


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




More information about the Python-list mailing list