[Python-ideas] Vectorization [was Re: Add list.join() please]

David Mertz mertz at gnosis.cx
Sun Feb 3 13:22:53 EST 2019


On Sun, Feb 3, 2019 at 3:54 AM Adrien Ricocotam <ricocotam at gmail.com> wrote:

> I think all the issues you have right now would go of using another
> operation. I proposed the @ notation that is clear and different from
> everything else,
>
plus the operator is called "matmul" so it completely makes sense. The the
> examples would be :
>


> >>> l = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split()
> >>> v = Vector(l)
> >>> len(v)
> 12
> >>> v @ len
> <Vector of [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]>
>

I cannot really see how using the @ operator helps anything here.  If this
were a language that isn't Python (or conceivably some future version of
Python, but that doesn't feel likely or desirable to me), I could imagine @
as an operator to vectorize any arbitrary sequence (or iterator).  But
given that we've already made the sequence into a Vector, there's no need
for extra syntax to say it should act in a vectorized way.

Moreover, your syntax is awkward for methods with arguments.  How would I
spell:

v.replace('foo', 'bar')


In the @ syntax? I actually made an error on my first pass where simply
naming a method was calling it.  I thought about keeping it for a moment,
but that really only allows zero argument calls.

I think the principled thing to do here is add the minimal number of
methods to Vector itself, and have everything else pass through as
vectorized calls.  Most of that minimal number are "magic method":
__len__(), __contains__(), __str__(), __repr__(), __iter__(),
__reversed__().  I might have forgotten a couple.  All of those should not
be called directly, normally, but act as magic for operators or built-in
functions.

I think I should then create regular methods of the same name that perform
the vectorized version.  So we would have:

len(v)   # -> 12

v.len()  # -> <Vector of [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]>

list(v)  # -> ["Jan", "Feb", "Mar", "Apr", "May", "Jul" ...]
v.list() # -> <Vector of [["J", "a", "n"], ["F", "e", "b"] ... >


I can't implement every single constructor that users might
conceivably want, of course, but I can do it for the basic types in
builtins and common standard library.  E.g. I might do:

v.deque() # -> <Vector of [deque(["J", "a", "n"]), deque(["F", "e", "b"])
... >


But I certainly won't manually add:

v.custom_linked_list()  # From my_inhouse_module.py


Hmm... maybe even I could look at names of maybe-constructors in the
current namespace and try them.  That starts to feel too magic.  Falling
back to this feels better:

map(custom_linked_list, v)  # From my_inhouse_module.py



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190203/45e7fbf6/attachment-0001.html>


More information about the Python-ideas mailing list