classification
Title: Use logical negation of integers directly in arithmatic propostions and equations
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Kasra Vand, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-04-24 07:31 by Kasra Vand, last changed 2018-04-24 13:21 by serhiy.storchaka. This issue is now closed.

Messages (7)
msg315688 - (view) Author: Kasra Vand (Kasra Vand) Date: 2018-04-24 07:31
Logical Negation of integers in Python always returns a Boolean result which can be achieve using `not`. Sometimes it's necessary to use this result directly in a proposition within a list comprehension (mostly). But if we use `not` directly in such arithmatic proposition it will raise a `SyntaxError.

```
In [46]: 3 + not(4)
  File "<ipython-input-46-e87dc6b7d800>", line 1
    3 + not(4)
          ^
SyntaxError: invalid syntax

```

Isn't that possible to make this work for integers in future releases? I was more curious to know what would be the drawbacks of such feature if there are any?
msg315689 - (view) Author: Kasra Vand (Kasra Vand) Date: 2018-04-24 07:35
This may seem not very useful while except 0 for other numbers it returns False but one may want to use another proposition inside `not` that can use either 0 or a nonzero number. Also, in this case we can form more comprehensive logical propositions.
msg315691 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-24 07:59
You can use 3 + (not 4).
msg315694 - (view) Author: Kasra Vand (Kasra Vand) Date: 2018-04-24 11:21
Thanks, Indeed. But my question is more about the syntax and why it's not that straight? The reasons for that is because Python is known as a scientific programming language and many people come with mathematical background and may want to use this kind of syntax.

So is it possible to add this feature?
msg315696 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-24 11:51
If `3 + not 4` be accepted, should it be evaluated to the same value as `not 4 + 3`? Currently the result of `not 4 + 3` is False.
msg315697 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-24 11:54
And consider the example `3 + not 4 + 5`.
msg315699 - (view) Author: Kasra Vand (Kasra Vand) Date: 2018-04-24 12:48
I think this will contradict to Python's operators precedence. What I mentioned seems like you're passing the number as an argument to `not`, and this is while `not` is not a function. There is an `operator.not_` function that does the same job as expected though.

Now I see why it's not efficient and wise to do so. This is violation of  operators precedence.
History
Date User Action Args
2018-04-24 13:21:20serhiy.storchakasetresolution: not a bug
2018-04-24 12:48:42Kasra Vandsetstatus: open -> closed
stage: resolved
2018-04-24 12:48:05Kasra Vandsetmessages: + msg315699
2018-04-24 11:54:19serhiy.storchakasetmessages: + msg315697
2018-04-24 11:51:53serhiy.storchakasetmessages: + msg315696
2018-04-24 11:21:47Kasra Vandsetmessages: + msg315694
2018-04-24 07:59:20serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg315691
2018-04-24 07:35:49Kasra Vandsetmessages: + msg315689
2018-04-24 07:31:12Kasra Vandcreate