mutable default parameter problem [Prothon]

Rob Williscroft rtw at freenet.co.uk
Wed Jun 16 09:52:04 CEST 2004


Andrea Griffini wrote in news:dqqvc0dbcfr7ns1bpqo9f5oqvg9us3o9o2 at 4ax.com 
in comp.lang.python:

>>def F(a, b, cache={}):
>>    try:
>>        return cache[(a,b)]
>>    except (IndexError, ):
>>        value = cache[(a,b)] = do_some_long_calc(a,b)
>>        return value
> 
> I'm new to python. To my eyes this is a pretty poor attempt to
> have static variables. I've implemented in the past a few
> scripting languages, and it's not really difficult to
> implement static variables...
> 

But python has static variables.

def another( x ):
  y = getattr( another, 'static', 10 )
  another.static = x
  return y

print another(1), another(2), another(4)


> it's quite surprising for me
> there's no such a concept in python and just that wart...
> hmmm... excuse me... that bad smelling wart has to be used instead.
> 

It seems to me in python "everything is an object" leads to
"everything is a dictionary" (except when it isn't:).

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/



More information about the Python-list mailing list