[EMAIL PROTECTED] writes:
>> Faster is:
>>class FastHashableList(list):
>> def __hash__(self):
>> return id(self)
> But that's wrong.
You're right - I should have fixed equality while I was at it. The
results may not be what you had in mind, but it's a perfectly
reasonable use case.
>> The
> Faster is:
>
>class FastHashableList(list):
> def __hash__(self):
> return id(self)
But that's wrong. Two equal objects must hash to the same value, and
you're not guaranteeing that at all. My degenerate __hash__ does
guarantee that, and I still claim the performance impact from
collision
[EMAIL PROTECTED] writes:
> class HashableList(list):
> def __hash__(self):
> return 0
This will suck for performance if you put a lot of lists in the same
dictionary. Faster is:
class FastHashableList(list):
def __hash__(self):
return id(self)
> I think python is broken
On Wed, 4 Jan 2006 [EMAIL PROTECTED] wrote:
> I think python is broken here-- why aren't lists hashable, or why isn't
> there a straightforward way to make memoised() work?
a = [1, 2, 3]
d = {a: "foo"}
a[0] = 0
print d[a]
I feel your pain, but i don't think lists (and mutable objects generally)
But while we're at it, how about that unhashable object problem?
@memoised
def func(x, i):
return x[i]
L = [1,2,3]
print func(L, 1) # TypeError: list objects are unhashable
What's the deal? My func can certainly be memoized (but possibly with a
slower lookup depending on how many args are
On Sat, 31 Dec 2005 [EMAIL PROTECTED] wrote:
>just> I actually prefer such a global variable to the default arg
>just> trick. The idiom I generally use is:
>
>just> _cache = {}
>just> def func(x):
>just> result = _cache.get(x)
>just> if result is None:
>just>
On Sat, 31 Dec 2005 09:23:05 +0100, Just wrote:
> There's no need to declare _cache as global, since you're not assigning
> to it. So this global isn't all that pesky after all...
It is still a global variable, with all the potential Badness thereof,
even if you don't have to declare it.
--
On Fri, 30 Dec 2005 21:08:29 -0800, Raymond Hettinger wrote:
> Steven D'Aprano wrote:
>> I was playing around with simple memoization and came up with something
>> like this:
[snip]
> Try something like this:
>
> def func(x, _cache={}):
> if x in cache:
> return cache[x]
> resul
just> I actually prefer such a global variable to the default arg
just> trick. The idiom I generally use is:
just> _cache = {}
just> def func(x):
just> result = _cache.get(x)
just> if result is None:
just> result = x + 1 # or a time consuming calculati
In article <[EMAIL PROTECTED]>,
Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> I was playing around with simple memoization and came up with something
> like this:
>
> _cache = {}
> def func(x):
> global _cache
There's no need to declare _cache as global, since you're not assigning
to it. So
Steven D'Aprano wrote:
> I was playing around with simple memoization and came up with something
> like this:
>
> _cache = {}
> def func(x):
> global _cache
> if _cache.has_key(x):
> return _cache[x]
> else:
> result = x+1 # or a time consuming calculation...
>
Steven D'Aprano wrote:
> I was playing around with simple memoization and came up with something
> like this:
>
> _cache = {}
> def func(x):
> global _cache
> if _cache.has_key(x):
> return _cache[x]
> else:
> result = x+1 # or a time consuming calculation...
>
Steven D'Aprano wrote:
> I was playing around with simple memoization and came up with something
> like this:
>
> _cache = {}
> def func(x):
> global _cache
> if _cache.has_key(x):
> return _cache[x]
> else:
> result = x+1 # or a time consuming calculation...
>
13 matches
Mail list logo