I cannot evaluate this statement...

John Machin sjmachin at lexicon.net
Fri Mar 7 16:29:49 EST 2008


On Mar 8, 7:38 am, waltbrad <waltb... at hotmail.com> wrote:
> The script comes from Mark Lutz's Programming Python.  It is the
> second line of a script that will launch a python program on any
> platform.
>
> import os, sys
> pyfile = (sys.platform[:3] == 'win' and 'python.exe') or 'python'
>
> Okay, run on a win32 machine, pyfile evaluates to python.exe
>
> That makes sense. Because the first condition is true and 'python.exe'
> is true. So the next comparison is 'python.exe' or 'python'  Well,
> python.exe is true. So that value is returned to pyfile.
>
> Now. Run this on linux. The first condition evaluates sys.platform[:3]
> == 'win' as false. So, the next comparison should be  'False' or
> 'python'   -- This is because 'and' returns the first false value.

The next comparison is NOT ('False' or 'python'); it is (False or
'python'). 'False' is NOT false, 'False' (like any string of non-zero
length) is true.

(trueobject and expression) evaluates to the value of expression.
(falseobject and expression) evaluates to falseobject [and doesn't
evaluate expression].
(trueobject or expression) evaluates to trueobject [and doesn't
evaluate expression].
(falseobject or expression) evaluates to the value of expression.

So:
('NOT-win' == 'win' and 'python.exe') or 'python'
(False and 'python.exe') or 'python'
False or 'python'
'python'

> But, again, on linux pyfile evaluates to python.exe

Does it? Have you tried it?

>
> Where am I going wrong.  And when will this statment make pyfile
> evaluate to 'python' ?




More information about the Python-list mailing list