Long Boolean expressions (was [Edu-sig] Kirby Urner's Sieve of
Eratosthenes)
Gordon Worley
redbird@rbisland.cx
Mon, 5 Jun 2000 23:20:22 -0400
At 12:01 PM -0400 6/5/2000, John Posner <jjp@connix.com> wrote:
>NOTE 1: For clarity, I think the long-winded forms of "while" and "lambda"
>are preferable. That is:
>
> while len(nums) > 0: <-- instead of --> while nums:
> lambda n: n % prm > 0 <-- instead of --> lambda n: n % prm
>
>The short forms take advantage of a quirk of Python: the Boolean value TRUE
>is implemented as non-zero/non-empty.
I can't say that I agree. First of all, this is not a quirk of
Python, this is a feature. It is also present in many, many other
programming languages. Secondly, this is a style widely used by
Python hackers and other end-users (of Python, not of Python
scripts), so while the long form might be appropriate in the context
of learning Python syntax, it is not during application, except, of
course, by end-users who are having difficulty with the more concise
formation. Thus, using the long form only hurts others by not
forcing them to learn the style that they are going to encounter when
reading other people's code. Finally, while the '> 0' bit part works
in this situation, it is not very robust since any, to quote,
"non-zero/non-empty" is true. Thus, '!= 0' should be used instead of
'> 0', so that this long form will work in all situations.
--
- Gordon Worley
http://www.rbisland.cx/
mailto:redbird@rbisland.cx