On Mon, Jun 26, 2017 at 09:55:19AM -0700, David Mertz wrote:
> On Sun, Jun 25, 2017 at 8:23 PM, Steven D'Aprano <st...@pearwood.info>
> wrote:
> 
> > On Sun, Jun 25, 2017 at 02:06:54PM +0200, lucas via Python-ideas wrote:
> >
> > I have a counter-proposal: introduce the iterator chaining operator "&":
> >
> >     iterable & iterable --> itertools.chain(iterable, iterable)
> >
> 
> In [1]: import numpy as np
> In [2]: import itertools
> In [3]: a, b = np.array([1,2,3]), np.array([4,5,6])
> In [4]: a & b
> Out[4]: array([0, 0, 2])
> In [5]: a + b
> Out[5]: array([5, 7, 9])
> In [6]: list(itertools.chain(a, b))
> Out[6]: [1, 2, 3, 4, 5, 6]
> 
> These are all distinct, useful, and well-defined behaviors.

Um... yes? I don't understand what point you are making.


Did you read all of my post? I know it was long, but if you stopped 
reading at the point you replied, you might not realise that my proposal 
keeps the existing bitwise-AND behaviour of & and so the numpy array 
behaviour won't change.

TL;DR

- keep the existing __and__ and __rand__ behaviour;

- if they are not defined, and both operands x, y are iterable, 
  return chain(x, y);

- raise TypeError for operands which neither support __(r)and__ 
  nor are iterable.


I think that chaining iterators is common enough and important enough in 
Python 3 to deserve an operator. While lists are still important, a lot 
of things which were lists are now lazily generated iterators, and we 
often need to concatenate them. itertools.chain() is less convenient 
than it should be.

If we decide that chaining deserves an operator, it shouldn't be + 
because that clashes with existing sequence addition. & has the 
advantage that it means "concatenation" in some other languages, it 
means "and" in English which can be read as "add or concatenate", and it 
is probably unsupported by most iterables.

I didn't think of numpy arrays as an exception (I was mostly thinking of 
sets) but I don't think people chain numpy arrays together very often. 
If they do, it's easy enough to call iter() first.


-- 
Steve
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to