[Python-ideas] [Python-Dev] the role of assert in the standard library ?
Tarek Ziadé
ziade.tarek at gmail.com
Thu Apr 28 14:27:14 CEST 2011
This is a thread from python-dev I am moving to python-ideas, because
I want to debate the "assert" keyword :)
On Thu, Apr 28, 2011 at 12:27 PM, Michael Foord
<fuzzyman at voidspace.org.uk> wrote:
> On 28/04/2011 09:34, Terry Reedy wrote:
>>
>> On 4/28/2011 3:54 AM, Tarek Ziadé wrote:
>>>
>>> Hello
>>>
>>> I removed some assert calls in distutils some time ago because the
>>> package was not behaving correctly when people were using Python with
>>> the --optimize flag. In other words, assert became a full part of the
>>> code logic and removing them via -O was changing the behavior.
>>>
>>> In my opinion assert should be avoided completely anywhere else than
>>> in the tests. If this is a wrong statement, please let me know why :)
>>
>> My understanding is that assert can be used in production code but only to
>> catch logic errors by testing supposed invariants or postconditions. It
>> should not be used to test usage errors, including preconditions. In other
>> words, assert presence or absence should not affect behavior unless the code
>> has a bug.
But it does affect the behaviour at the end: when the code has a bug,
then the way the code works is affected and differs depending if -O is
used.
Let me take an example:
bucket = []
def add(stuff):
bucket.append(stuff)
def purge_and_do_something():
bucket.clear()
assert len(bucket) == 0
... do something by being sure the bucket is empty ...
here, we could say assert is legitimate, as it just checks a
post-condition. But this code is not thread-safe and if it's run via
several threads, some code could add something in the bucket while
purge() check for the assertion.
So, if I run this code using -O it will not behave the same: it will
seem to work. I am not arguing against the fact that this code should
be changed and set up a lock.
My point is that I do not understand why there are assert calls to
check post-conditions. Those seem to be relics from the developer that
marked something she needed to take care of in her code, but did not
yet. e.g. like a TODO or a XXX or a NotImplementedError
Moreover, why -O and -OO are removing assertions in that case ? even
if the assert is "supposed not to happen" it's present in the code and
can happen (or well, remove it). So "optimize" potentially changes the
behaviour of the code.
>From what I understood so far, a line with an assert should be
considered as a dead code if we want the code to behave always the
same way. I remove dead code in my code...
Cheers
Tarek
--
Tarek Ziadé | http://ziade.org
More information about the Python-ideas
mailing list