At Tuesday 26/12/2006 21:06, Steven D'Aprano wrote:
> It just feels so ugly to use try/except to enable the variable but I've
> found it useful at least once.
That's a matter of taste. Try replacing the try...except block with
hasattr:
def doStuff():
if hasattr(doStuff, timesUsed):
doStuff.timesUsed += 1
else:
doStuff.timesUsed = 1
do_common_code
Using hasattr you end checking every access to "timesUsed" twice.
Using a try/except, after the initial setup, there is no additional overhead.
hasattr does exactly that: it calls getattr to see if it raises an
exception (this is documented behavior, not a hidden implementation detail).
In Python it's far more common EAFP ('easier to ask for forgiveness
than permission') than LBYL ('look before you leap').
--
Gabriel Genellina
Softlab SRL
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
--
http://mail.python.org/mailman/listinfo/python-list