Persistent variables in python

Gabriel Genellina gagsl-py at yahoo.com.ar
Tue Dec 26 17:47:08 EST 2006


At Tuesday 26/12/2006 19:13, buffi wrote:

>def doStuff():
>   try:
>     #Will throw exception if not set
>     doStuff.timesUsed
>
>Is this concidered bad coding practice since I guess persistent
>variables in functions are not meant to be?

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...

If you need to code something special for the 2nd and following 
calls, add an else: clause


-- 
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 




More information about the Python-list mailing list