The return of None-aware operators for Python
The return of None-aware operators for Python
Posted Jan 7, 2024 14:21 UTC (Sun) by cpitrat (subscriber, #116459)In reply to: The return of None-aware operators for Python by NYKevin
Parent article: The return of None-aware operators for Python
May I interest you in assert?
Posted Jan 8, 2024 22:06 UTC (Mon)
by NYKevin (subscriber, #129325)
[Link] (12 responses)
(Also, you can't specify the exception type, but that's small potatoes in comparison.)
Posted Jan 8, 2024 22:13 UTC (Mon)
by khim (subscriber, #9252)
[Link] (4 responses)
I have just tested and that doesn't happen. What version of python are you using??? I have never observed that effect in Python, but there are many implementations, maybe one of them does that, but for me it's reason not to use it rather then change use of
Posted Jan 8, 2024 22:22 UTC (Mon)
by mb (subscriber, #50428)
[Link]
$ cat t.py
Posted Jan 8, 2024 23:19 UTC (Mon)
by NYKevin (subscriber, #129325)
[Link] (1 responses)
Posted Jan 8, 2024 23:26 UTC (Mon)
by NYKevin (subscriber, #129325)
[Link]
As far as I can tell, 1.4 did not have an assert statement, so the assert statement has never been unconditional in any released version of Python.
Posted Jan 8, 2024 23:40 UTC (Mon)
by ABCD (subscriber, #53650)
[Link]
Posted Jan 8, 2024 23:36 UTC (Mon)
by marcH (subscriber, #57642)
[Link] (1 responses)
> so the assert is just for documentation purposes (to inform the next person who reads the code that the invariant exists and is important).
Small contradiction here.
I didn't know about the -O flag and I've always "run" asserts and every time one is hit it is massively more useful than a comment!
> so the implication is that your code is expected to be correct even when asserts are not run.
Agreed.
Posted Jan 8, 2024 23:37 UTC (Mon)
by NYKevin (subscriber, #129325)
[Link]
Sorry, my brain is smaller than yours, can you please elaborate?
Posted Jan 9, 2024 11:28 UTC (Tue)
by cpitrat (subscriber, #116459)
[Link]
But you can easily define your own raiseIfNone.
Posted Jan 9, 2024 15:11 UTC (Tue)
by atnot (subscriber, #124910)
[Link] (3 responses)
Posted Jan 9, 2024 15:28 UTC (Tue)
by kleptog (subscriber, #1183)
[Link]
Posted Jan 9, 2024 19:13 UTC (Tue)
by NYKevin (subscriber, #129325)
[Link]
Posted Jan 10, 2024 16:28 UTC (Wed)
by laarmen (subscriber, #63948)
[Link]
The return of None-aware operators for Python
> assert is not properly used for any purpose other than as a "live comment" (i.e. a comment that actually gets executed). This is because the -O flag disables asserts, so the implication is that your code is expected to be correct even when asserts are not run.
The return of None-aware operators for Python
assert
.The return of None-aware operators for Python
assert 2+2==5
$ python3.11 t.py
[...]
AssertionError
$ python3.11 -O t.py
The return of None-aware operators for Python
The return of None-aware operators for Python
The return of None-aware operators for Python
The return of None-aware operators for Python
The return of None-aware operators for Python
The return of None-aware operators for Python
The return of None-aware operators for Python
The return of None-aware operators for Python
The return of None-aware operators for Python
The return of None-aware operators for Python