Persistent variables in python
eduardo.padoan at gmail.com
eduardo.padoan at gmail.com
Wed Dec 27 13:11:11 EST 2006
> 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
>
Ok, it is a matter of taste and I prefer the try/except way, but with
hasattr you will do a function call and a intern 'truthness'
verification each time you increment .timeUsed. With try/except, after
the first time, you do nothing else than increment it.
More information about the Python-list
mailing list