[Python-ideas] Python multi-dimensional array constructor
Matt Gilson
matt at getpattern.com
Wed Oct 19 16:47:11 EDT 2016
FWIW, you probably _don't_ want to use `ndarray` directly. Normally, you
want to use the `np.array` factory function...
>>> import numpy as np
>>> a = np.ndarray([0, 1, 2])
>>> a
array([], shape=(0, 1, 2), dtype=float64)
Aside from that, my main problem with this proposal is that it seems to
only be relevant when used in third party code. There _is_ some precedence
for this (for example rich comparisons and the matrix multiplication
operator) -- However, these are all _operators_ so third party code can
hook into it using the provided hook methods. This proposal is different
in that it _isn't_ proposing an operator, so there isn't any object on
which to define a magic hook method. I think that it was mentioned that it
might be possible for a user to _register_ a callable that would then be
used when this syntax was envoked -- But having a global setting like that
leads to contention. What if I want to use this syntax with `np.ndarray`
but some other third party code (that I want to use _with_ numpy_ tries to
hook into the syntax as well? All of a sudden, my script stops working as
soon as I import a new third party module.
I _do_ think that this might be a valid proposal for some of the more
domain specific python variants (e.g. IPython) which have a pre-processing
layer on top of the rest of the language. It might be worth trying to
float this idea in one of their ideas mailing lists/issue trackers.
On Wed, Oct 19, 2016 at 1:10 PM, Todd <toddrjen at gmail.com> wrote:
> On Wed, Oct 19, 2016 at 3:55 PM, Joseph Jevnik <joejev at gmail.com> wrote:
>
>> You could add or prototype this with quasiquotes (
>> http://quasiquotes.readthedocs.io/en/latest/). You just need to be able
>> to parse the body of your expression as a string into an array. Here is a
>> quick example with a parser that only accepts 2d arrays:
>>
>> ```
>> # coding: quasiquotes
>>
>> import numpy as np
>> from quasiquotes import QuasiQuoter
>>
>>
>> @object.__new__
>> class array(QuasiQuoter):
>> def quote_expr(self, expr, frame, col_offset):
>> return np.array([
>> eval('[%s]' % d, frame.f_globals, frame.f_locals)
>> for d in expr.split('||')
>> ])
>>
>>
>> def f():
>> a = 1
>> b = 2
>> c = 3
>> return [$array| a, b, c || 4, 5, 6 |]
>>
>>
>> if __name__ == '__main__':
>> print(f())
>> ```
>>
>
> Interesting project, thanks! If there is any actual interest in this that
> might be a good way to prototype it.
>
>
>> Personally I am not sold on replacing `[` and `]` with `|` because I like
>> that you can visually see where dimensions are closed.
>>
>>
> Yes, that issue occurred to me. But assuming a rectangular matrix, I had
> trouble coming up with a good example that is clearer than what you could
> do with this syntax. For simple arrays it isn't needed, and complicated
> arrays are large so picking out the "[" and "]" becomes visually harder at
> least for me. Do you have a specific example that you think would be
> clearer than what is possible with this syntax?
>
> Of course that is more of an issue with jagged arrays, but numpy doesn't
> support those and I am not aware of any plans to add them (dynd is another
> story).
>
> Also keep in mind that this would supplement the existing approach, it
> doesn't replace it. np.ndarray() would stay around just like list() stays
> around for cases where it makes sense.
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
--
[image: pattern-sig.png]
Matt Gilson // SOFTWARE ENGINEER
E: matt at getpattern.com // P: 603.892.7736
We’re looking for beta testers. Go here
<https://www.getpattern.com/meetpattern> to sign up!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161019/7c292a37/attachment-0001.html>
More information about the Python-ideas
mailing list