Hi,
Recently the idea has come to me that walrus operator can have some issues when
use in assertions.
I have been taught that assertions must not produce side effects at all. This
is a problem in languages such as C. But not in Python (until 3.8) since it
does not allow assignment expressions. But now, for best or worst, we have the
walrus operator and a code like this is possible:
`python3.8 -c "assert (always := True); print(f'{always=}')"`
Which outputs:
```
always=True
```
since the assertion is always true.
However, if assertions are disabled:
`python3.8 -Oc "assert (always := True); print(f'{always=}')"`
it raises an exception:
```
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'always' is not defined
```
because of the side effect caused by the walrus operator.
Do not you think that disabling walrus operators in assertions would be a good
idea? Would be it possible?
Personally, I do not see many advantages in walrus operator (versus
disadvantages, of course). However, in the case of assertions, a nice and
secure feature of Python has been lost without any apparent benefit.
Thank you.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/KJFG76RXMZ2YVNZ4WXYY4TTQWSDJSMGY/
Code of Conduct: http://python.org/psf/codeofconduct/