Unexpected Python Behavior
Andrew Dalke
adalke at mindspring.com
Mon Oct 4 20:05:18 EDT 2004
Fernando Perez wrote:
> Sorry to pitch in late, and forgive me if this has already been discussed and
> thrown out for some other reason, I didn't follow the whole thread. Why not
> use function attributes for the cache?
>
> In [3]: def foo(x):
> ...: if x in foo.cache:
> ...: print 'x=',x,'in cache!'
> ...: else:
> ...: print 'new calculation for x:',x
> ...: foo.cache[x] = 1
> ...:
>
> In [4]: foo.cache={}
It was discussed. Here's Alex's comments
] def f(x):
] if x in f.cache: ...
] f.cache = []
]
] which among other things suffers from f.cache having to be
] used in spots that come lexically before it's defined; or
] a decorator equivalent:
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list