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
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):
> 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
> 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
"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
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...
>
> 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
> 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):
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
Found out a quite fun way to store persistent variables in functions in
python.
def doStuff():
try:
#Will throw exception if not set
doStuff.timesUsed
##
#Insert stuff to do each time the function is called here
##
doStuff.timesUsed+=1
print "Function call!"
excep
10 matches
Mail list logo