#69 Cannot inherit from list and override __getitem__


When you inherit from the 'list' builtin type and
override __getitem__ (and similar functions) Psyco
works mysteriously and brokenly. The following code
fails with "AttributeError: 'int' object has no
attribute 'lower'" in Psyco 1.5.1, but works correctly
if you comment out psyco.full(). You can see that Psyco
is trying to call __getitem__ with key=0, which should
not happen.

import psyco

class ListDict(list):
def __getitem__(self, key):
print "Key is", key
key = key.lower()
values = [value for (k, value) in self if
k.lower() == key]
if not values: raise KeyError, key
else: return values

def keys(self):
return self and map(str.lower, set(zip(*self)[0]))

def values(self):
return map(self.__getitem__, self.keys())

def items(self):
return zip(self.keys(), self.values())

if __name__ == "__main__":
c = ListDict([("foo", "bar"), ("foo", "baz")])
print c.items()


  • Joe Wreschnig

    Joe Wreschnig - 2006-09-10

    Logged In: YES

    SourceForge eats indentation, I guess. Here's a usable version.

  • Joe Wreschnig

    Joe Wreschnig - 2006-09-10

    Small test case for getitem.

  • Armin Rigo

    Armin Rigo - 2006-09-10

    Logged In: YES

    Ok, should be fixed now.

    As a side note, I guess you have good reasons for not simply
    using a dict {"lowercasekey": ["list", "of", "values"]}.
    Unless the ListDicts are always very small, they don't seem
    to have the 'right' performance characteristic. The
    usual quote "Make your algorithms right first and then
    optimize its execution"...

  • Armin Rigo

    Armin Rigo - 2006-10-09
    • status: open --> closed-fixed

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks