On 09.05.20 12:18, Alex Hall wrote:
On Sat, May 9, 2020 at 11:57 AM Dominik Vilsmeier
<dominik.vilsme...@gmx.de <mailto:dominik.vilsme...@gmx.de>> wrote:
So as a practical step forward, what about providing a wrapper type
which performs all operations elementwise on the operands. So for
example:
if all(elementwise(chars) == string):
...
Here the `elementwise(chars) == string` part returns a generator which
performs the `==` comparison element-by-element.
Now `==` has returned an object that's always truthy, which is pretty
dangerous.
That can be resolved by returning a custom generator type which
implements `def __bool__(self): raise TypeError('missing r.h.s. operand')`.
This doesn't perform any length checks yet, so as a bonus one
could add
an `all` property:
if elementwise(chars).all == string:
...
This is now basically numpy.
```
In[14]: eq = numpy.array([1, 2, 3]) == [1, 2, 4]
In[15]: eq
Out[15]: array([ True, True, False])
In[16]: eq.all()
Out[16]: False
In[17]: eq.any()
Out[17]: True
In[18]: bool(eq)
Traceback (most recent call last):
...
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
```
I've used number instead of strings because numpy treats strings as
units instead of iterables for this kind of purpose, so you'd have to
do some extra wrapping in lists to explicitly ask for character
comparisons.
Actually I took some inspiration from Numpy but the advantage is of
course not having to install Numpy. The thus provided functionality is
only a very small subset of what Numpy provides.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/BDUA3W47HMXVHWPI5XTFUP2JYNBR5M5J/
Code of Conduct: http://python.org/psf/codeofconduct/