sequence multiplied by -1
pavlovevidence at gmail.com
Mon Oct 4 21:39:02 CEST 2010
On Oct 4, 9:14 am, Ethan Furman <et... at stoneleaf.us> wrote:
> Antoon Pardon wrote:
> > Suppose you write your class so that '+' will provide addition and
> > you provide a method concat to concatenate. Now no matter how usefull
> > the sequence library would be for you, you can't use it. You will
> > have to rewrite those function you need, in terms of a concat method
> > instead of '+'.
> > That also means that whatever sequence like functions you write
> > for this class, you can't use those functions for sequences in
> > general because in general, sequence don't have a concat method.
> Do you have an example of where this would be useful?
I gave an example elsewhere in the thread. I'll repeat:
Numpy arrays can usefully define addition and concatenation
operations. Numpy uses + for elementwise addition, and a function
called concatenate for concatenation. If Python lists used a separate
concatenation operator, then Numpy arrays could use that for
But the point isn't that this is useful so much as that combining the
operations is detrimental.
Numpy arrays use + for elementwise addition, whereas Python lists use
+ for concatenation. Consequently, if you write a function that uses
+ for concatenation you can't use it with Numpy arrays, and you could
even get silent errors if the operation doesn't raise an exception.
This is why using + for two different things hurts duck typing. Numpy
arrays quack like a duck, but they aren't ducks. That's bad.
The point is, the reason to have separate operators for addition and
concatenation is not that it's so wonderfully useful, it's that it
avoids serious drawbacks.
More information about the Python-list