polymorphjsm &c (was Re: I come to praise .join, not to bury it...)

Carel Fellinger cfelling at iae.nl
Tue Mar 6 19:06:13 EST 2001


once Alex Martelli wrote:
...
> that's a different flamewar ("form follows function" crusaders,
> this IS a call to arms!-).

so you wanna fight he,

and else were he wrote
... bunch of disgustingly true ramblings on polymorphisme snipped


and now he comes with
> class MixedJoiner:
...
> Thanks to the fully general polymorphism that joiner.join affords,
> we can then use a *special* joiner-object to do fancy joining.

So let's reverse things:

>>> class MixedSplitter:
...     def __init__(self, splitter1, splitter2):
...         self.splitter1, self.splitter2 = splitter1, splitter2
...     def split(self, sequence):
...         s = self.splitter1(sequence)
...         return s[:-1] + self.splitter(s[-1])
... 
>>> ms = MixedSplitter(', ', ' and ')
>>> ms.split('1, 2, 3 and 4')
some nasty errormessage snipped


This won't work with Python2.
Ofcourse we could ammend MixedSplitter's split method like:

>>> def split(self, sequence):
...     s = sequence.split(self.splitter1)
...     return s[:-1] + s[-1].split(self.splitter2)
...
>>> MixedSplitter.split = split
>>> ms.split('1, 2, 3 and 4')
['1', '2', '3', '4']

But the real evil is that no code out there will allow us to easily
use the polimorphic nature of this splitter.


The moral being that we shouldn't argue about the uglines joiner.join
but of the lack of a tru polymorphic splitter.split!
-- 
groetjes, carel



More information about the Python-list mailing list