
See the following testing in IPython shell: In [6]: import numpy as np In [7]: a = np.array([1], dtype=(bool)) In [8]: b = np.array([1], dtype=bool) In [9]: a Out[9]: array([ True]) In [10]: b Out[10]: array([ True]) It seems that dtype=(bool) and dtype=bool are both correct usages. If so, which is preferable? Regards, HZ

On Tue, Oct 19, 2021 at 3:42 PM <hongyi.zhao@gmail.com> wrote:
This is not really a numpy issue: in Python itself `(bool)` is the exact same thing as `bool`. Superfluous parentheses are just ignored. You could use `dis.dis` to compare the two expressions and see that they compile to the same bytecode. So go with `dtype=bool`. András

You could use `dis.dis` to compare the two expressions and see that they compile to the same bytecode.
Do you mean the following: In [1]: import numpy as np In [2]: from dis import dis In [7]: dis('bool') 1 0 LOAD_NAME 0 (bool) 2 RETURN_VALUE In [8]: dis('(bool)') 1 0 LOAD_NAME 0 (bool) 2 RETURN_VALUE

On Tue, Oct 19, 2021 at 4:07 PM <hongyi.zhao@gmail.com> wrote:
Indeed, that is exactly what I meant. You don't even need the numpy import for that. Since `bool` and `(bool)` are compiled to the same bytecode, numpy isn't even aware that you put parentheses around `bool` in your code.

On Tue, Oct 19, 2021 at 8:22 PM <hongyi.zhao@gmail.com> wrote:
Do I have to use it this way?
Nothing is forcing you to, but everyone else will write it as `dtype=bool`, not `dtype=(bool)`. `dtype=(bool)` is perfectly syntactically-valid Python. It's just not idiomatic, so readers of your code will wonder why you wrote it that way and if you meant something else and will have trouble reading your code. -- Robert Kern

On Wed, Oct 20, 2021 at 8:29 AM Robert Kern <robert.kern@gmail.com> wrote:
I use Emacs, and find that python-lsp-server will give the dtype=() as the completion result, see here [1] for more detailed discussion. [1] https://github.com/python-lsp/python-lsp-server/issues/98 HZ

On Tue, Oct 19, 2021 at 8:54 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
Well, that's not helpful of python-lsp-server. Is it looking at our type annotations? The annotations for `dtype=` arguments are likely quite complicated because we accept so many different types here, but `()` is not a great completion. -- Robert Kern

On Tue, Oct 19, 2021 at 3:42 PM <hongyi.zhao@gmail.com> wrote:
This is not really a numpy issue: in Python itself `(bool)` is the exact same thing as `bool`. Superfluous parentheses are just ignored. You could use `dis.dis` to compare the two expressions and see that they compile to the same bytecode. So go with `dtype=bool`. András

You could use `dis.dis` to compare the two expressions and see that they compile to the same bytecode.
Do you mean the following: In [1]: import numpy as np In [2]: from dis import dis In [7]: dis('bool') 1 0 LOAD_NAME 0 (bool) 2 RETURN_VALUE In [8]: dis('(bool)') 1 0 LOAD_NAME 0 (bool) 2 RETURN_VALUE

On Tue, Oct 19, 2021 at 4:07 PM <hongyi.zhao@gmail.com> wrote:
Indeed, that is exactly what I meant. You don't even need the numpy import for that. Since `bool` and `(bool)` are compiled to the same bytecode, numpy isn't even aware that you put parentheses around `bool` in your code.

On Tue, Oct 19, 2021 at 8:22 PM <hongyi.zhao@gmail.com> wrote:
Do I have to use it this way?
Nothing is forcing you to, but everyone else will write it as `dtype=bool`, not `dtype=(bool)`. `dtype=(bool)` is perfectly syntactically-valid Python. It's just not idiomatic, so readers of your code will wonder why you wrote it that way and if you meant something else and will have trouble reading your code. -- Robert Kern

On Wed, Oct 20, 2021 at 8:29 AM Robert Kern <robert.kern@gmail.com> wrote:
I use Emacs, and find that python-lsp-server will give the dtype=() as the completion result, see here [1] for more detailed discussion. [1] https://github.com/python-lsp/python-lsp-server/issues/98 HZ

On Tue, Oct 19, 2021 at 8:54 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
Well, that's not helpful of python-lsp-server. Is it looking at our type annotations? The annotations for `dtype=` arguments are likely quite complicated because we accept so many different types here, but `()` is not a great completion. -- Robert Kern
participants (7)
-
Andras Deak
-
Benjamin Root
-
Hongyi Zhao
-
hongyi.zhao@gmail.com
-
Mansour Moufid
-
Robert Kern
-
Ryan Soklaski