Re: Persistent variables in python

2006-12-27 Thread Steven D'Aprano
On Wed, 27 Dec 2006 10:11:11 -0800, [EMAIL PROTECTED] wrote: > >> 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_c

Re: Persistent variables in python

2006-12-27 Thread Gabriel Genellina
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):

Re: Persistent variables in python

2006-12-27 Thread [EMAIL PROTECTED]
> 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

Re: Persistent variables in python

2006-12-27 Thread buffi
> There is a problem that this trick only works for functions and not for > methods as it assumes that there is a global name through which you can > access the function. I didn't really see any issue with this since methods can store the persistant data from the method inside the class containing

Re: Persistent variables in python

2006-12-27 Thread Duncan Booth
"buffi" <[EMAIL PROTECTED]> wrote: > Is this concidered bad coding practice since I guess persistent > variables in functions are not meant to be? > There is a problem that this trick only works for functions and not for methods as it assumes that there is a global name through which you can ac

Re: Persistent variables in python

2006-12-26 Thread Steven D'Aprano
On Tue, 26 Dec 2006 15:01:40 -0800, buffi wrote: >> def doStuff(some, arguments, may, *be, **required): >> try: >> doStuff.timesUsed += 1 >> except AttributeError: >> doStuff.timesUsed = 1 >> # ... special case for first call ... >> # ...common code... >

Re: Persistent variables in python

2006-12-26 Thread Lee Harr
> Found out a quite fun way to store persistent variables in functions in > python. > > Is this concidered bad coding practice since I guess persistent > variables in functions are not meant to be? I am using is in one of my recent projects. I was thinking of it sort of like "static" variables

Re: Persistent variables in python

2006-12-26 Thread buffi
> 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):

Re: Persistent variables in python

2006-12-26 Thread Gabriel Genellina
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