Why " ".some_string is often used ?

Christos TZOTZIOY Georgiou tzot at sil-tec.gr
Thu Jan 8 13:21:22 EST 2004


On 08 Jan 2004 16:34:39 +0100, rumours say that Syver Enstad
<syver-en+usenet at online.no> might have written:

>I'd also like a reversing method for len
>
>class MyList(list):
>   def len(self):
>        return len(self)

You can always use the __len__ attribute in this specific case.

And now for the hack value:

class MyList(list):
    import new as _new, __builtin__
    def __getattr__(self, attr):
        try:
            return self._new.instancemethod( \
                   getattr(self.__builtin__, attr), \
                   self, \
                   None)
        except AttributeError:
            raise AttributeError, \
                  "there is no '%s' builtin" % attr

allowing:

>>> a=MyList()
>>> a.append(12)
>>> a.append(24)
>>> a.len()
2
>>> a.min()
12
>>> a.max()
24

It works for all builtins that can take a list as a first argument.
Of course it should not be taken seriously :)
-- 
TZOTZIOY, I speak England very best,
Ils sont fous ces Redmontains! --Harddix



More information about the Python-list mailing list