Lua is known for using assignments in asserts in C.

It can come in handy for more complex, multi-part assertions.

On 2020-02-11 4:10 p.m., jdve...@gmail.com wrote:
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 -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/KJFG76RXMZ2YVNZ4WXYY4TTQWSDJSMGY/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UKJPOROMU4EUFJRH5JCL4VXHXJ4ZE7PL/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to