[Python-3000] Adaption & generic functions [was Generic functions]

Jim Jewett jimjjewett at gmail.com
Fri Apr 7 20:09:42 CEST 2006


On 4/5/06, Walter Dörwald <walter at livinglogic.de> wrote:

> class Sequence(Interface):
>     def getitem(self, index): pass
>     def len(self): pass

> class PythonSeqAsSequence(Sequence):
>     def __init__(self, obj):
>        self.obj = obj

>     def getitem(self, index):
>        return self.obj[i]

>     def len(self):
>        return len(self.obj)

> Sequence.register(PythonSeqAsSequence, list)


> But if adapting is done via __call__() we have a problem: Sequence
> already provides a __call__, the constructor.

So?  I can't see any reason why you would want to construct an
(abstract) Sequence.  What would it mean?

What you might want to do is to construct a specific Sequence, such as
Sequence([1,2,3]), or the empty Sequence() .

Just as the list() constructor takes arguments and returns something
that looks like a list, the Sequence() constructor should take an
argument and return something that looks like a sequence.

The difference from a "regular" class constructor is that you are less
likely to create a new object if it can be done more efficiently, and
there is no expectation that Sequence will appear in the object's mro.

Actually calling the sole method of an interface is admittedly more debatable.

-jJ


More information about the Python-3000 mailing list