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:
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?
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
Regards, HZ _______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: deak.andris@gmail.com
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:
You could use `dis.dis` to compare the two expressions and see that they compile to the same bytecode.
Do you mean the following:
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.
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 _______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: deak.andris@gmail.com
As he said: this is not the appropriate use for this mailing list. On Tue, Oct 19, 2021, 10:05 AM <hongyi.zhao@gmail.com> wrote:
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 _______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: rsoklaski@gmail.com
(something) in python is only needed if you need to change the order of precedence or if you need to split something across 2 or more lines. Otherwise, it has no meaning and it is extraneous. On Tue, Oct 19, 2021 at 9:42 AM <hongyi.zhao@gmail.com> wrote:
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 _______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: ben.v.root@gmail.com
On Tue, Oct 19, 2021 at 9:43 AM <hongyi.zhao@gmail.com> wrote:
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?
For a one-element tuple, add a comma:
bool <class 'bool'> (bool) <class 'bool'> (bool,) (<class 'bool'>,)
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:
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.
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:
On Wed, Oct 20, 2021 at 8:29 AM Robert Kern <robert.kern@gmail.com> wrote:
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.
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
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