Persistent variables in python

buffi bjorn.kempen at gmail.com
Tue Dec 26 18:01:40 EST 2006


> I don't think so, since Python proudly says that functions are
> first-class objects.
> CherryPy does a similar thing to mark a method as "exposed".
>
> But perhaps I'd write the code this way to avoid an unneeded and
> risky recursive call:
>
> def doStuff(some, arguments, may, *be, **required):
>      try:
>          doStuff.timesUsed += 1
>      except AttributeError:
>          doStuff.timesUsed = 1
>          # ... special case for first call ...
>      # ...common code...

True, the recursivity is not needed there I guess :)

It just feels so ugly to use try/except to enable the variable but I've
found it useful at least once.

/buffi (buffis.com)




More information about the Python-list mailing list