Using scalar constructors to produce arrays

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Using scalar constructors to produce arrays

Joshua Wilson
Over in the NumPy stubs there's an issue

https://github.com/numpy/numpy-stubs/issues/41

which points out that you can in fact do something like

```
np.float32([1.0, 0.0, 0.0])
```

to construct an ndarray of float32. It seems to me that though you can
do that, it is not a best practice, and one should instead do

```
np.array([1.0, 0.0, 0.0], dtype=np.float32)
```

Do people agree with that assessment of what the best practice is? If
so, it seems to make the most sense to continue banning constructs
like `np.float32([1.0, 0.0, 0.0])` in the type stubs (as they should
promote making easy-to-understand, scalable NumPy code).

- Josh
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Using scalar constructors to produce arrays

ralfgommers


On Sun, Apr 19, 2020 at 8:47 PM Joshua Wilson <[hidden email]> wrote:
Over in the NumPy stubs there's an issue

https://github.com/numpy/numpy-stubs/issues/41

which points out that you can in fact do something like

```
np.float32([1.0, 0.0, 0.0])
```

to construct an ndarray of float32. It seems to me that though you can
do that, it is not a best practice, and one should instead do

```
np.array([1.0, 0.0, 0.0], dtype=np.float32)
```

Do people agree with that assessment of what the best practice is? If
so, it seems to make the most sense to continue banning constructs
like `np.float32([1.0, 0.0, 0.0])` in the type stubs (as they should
promote making easy-to-understand, scalable NumPy code).

+1 for banning that construct, that's really ugly

Cheers,
Ralf


_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Using scalar constructors to produce arrays

Sebastian Berg
On Sun, 2020-04-19 at 21:07 +0200, Ralf Gommers wrote:

> On Sun, Apr 19, 2020 at 8:47 PM Joshua Wilson <
> [hidden email]>
> wrote:
>
> > Over in the NumPy stubs there's an issue
> >
> > https://github.com/numpy/numpy-stubs/issues/41
> >
> > which points out that you can in fact do something like
> >
> > ```
> > np.float32([1.0, 0.0, 0.0])
> > ```
> >
> > to construct an ndarray of float32. It seems to me that though you
> > can
> > do that, it is not a best practice, and one should instead do
> >
> > ```
> > np.array([1.0, 0.0, 0.0], dtype=np.float32)
> > ```
> >
> > Do people agree with that assessment of what the best practice is?
> > If
> > so, it seems to make the most sense to continue banning constructs
> > like `np.float32([1.0, 0.0, 0.0])` in the type stubs (as they
> > should
> > promote making easy-to-understand, scalable NumPy code).
> >
>
> +1 for banning that construct, that's really ugly
>
I personally always considered it bad-practice. Unfortunately, I think
it may not be be uncommon use, so I am not sure we should spend our
deprecation chips/pain on it (if someone wants to try, we can see).

But at least in my opinion it should not be advertised or used in
docs/tutorials, and thus also not typed.

- Sebastian


> Cheers,
> Ralf
> _______________________________________________
> NumPy-Discussion mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/numpy-discussion


_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

signature.asc (849 bytes) Download Attachment