instance variable weirdness
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Fri Apr 14 14:03:28 EDT 2006
On Fri, 14 Apr 2006 13:30:49 -0300, Felipe Almeida Lessa wrote:
> Em Sex, 2006-04-14 às 09:18 -0700, wietse escreveu:
>> def __init__(self, name, collection=[]):
>
> Never, ever, use the default as a list.
Unless you want to use the default as a list.
Sometimes you want the default to mutate each time it is used, for example
that is a good technique for caching a result:
def fact(n, _cache=[1, 1, 2]):
"Iterative factorial with a cache."
try:
return _cache[n]
except IndexError:
start = len(_cache)
product = _cache[-1]
for i in range(start, n+1):
product *= i
_cache.append(product)
return product
--
Steven.
More information about the Python-list
mailing list