question of style

Lie Ryan lie.1296 at gmail.com
Fri Jul 3 02:40:19 EDT 2009


Paul Rubin wrote:
> Tim Harig <usernet at ilthio.net> writes:
>> That being the case, it might be a good idea either to handle the situation
>> and raise an exception or add:
>>
>> assert self.lower <= self.higher
>>
>> That way an exception will be raised if there is an error somewhere else
>> in the code rather then silently passing a possibly incorrect value.
> 
> Well, that assert is not right because you have to handle the case
> where one of the values is None.  The general sentiment is reasonable
> though, if you're concerned that the precondition may not be valid.

Well, it depends on where you put the assert statement. If your code is
like this:

if self.higher is self.lower is None: return
if self.lower is None: return self.higher
if self.higher is None: return self.lower
assert self.lower <= self.higher

then the assert statement is correct.

Some people might prefer to keep all asserts at the top of function though.



More information about the Python-list mailing list