Coding style
Antoon Pardon
apardon at forel.vub.ac.be
Thu Jul 20 03:32:02 EDT 2006
On 2006-07-19, Georg Brandl <g.brandl-nospam at gmx.net> wrote:
> Antoon Pardon wrote:
>
>>> Other than in PHP, Python has clear rules when an object of a builtin type
>>> is considered false (i.e. when it's empty). So why not take advantage of
>>> this?
>>
>> Because it doesn't always do what I want.
>>
>> I once had a producer consumer code. When the client asked whether new
>> items were available the function could return three different values
>>
>> 1) a list with items, to be consumed
>> 2) an empty list (meaning there were no items available for the
>> moment but there could be in the future
>> 3) None (meaning the producer was done)
>>
>> Just testing for the truth value of the returned result in order
>> to see whether the client should continue or not would often
>> have made the client exit prematurely.
>>
>> IME such cases where testing for the truth value of an object
>> don't give the expected result, happen often enough to make me
>> carefully think about what I want to test for and then explicitly
>> do so.
>
> You're right, carefully thinking is always a good thing. Not using a
> language feature just because it would fail in another case is not.
I would say the opposite is just as true. Using a language feature
just because it happens to give the right answer, is not a good thing.
You don't know how your code will evolve and my experience is that the
lesser you use this feature, the lesser the chance you will have
to track a nasty bug later.
--
Antoon Pardon
More information about the Python-list
mailing list