I cannot evaluate this statement...

Chris Mellon arkanes at gmail.com
Fri Mar 7 16:06:37 EST 2008


On Fri, Mar 7, 2008 at 2:38 PM, waltbrad <waltbrad 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.
>  But, again, on linux pyfile evaluates to python.exe
>
>  Where am I going wrong.  And when will this statment make pyfile
>  evaluate to 'python' ?
>  --
>  http://mail.python.org/mailman/listinfo/python-list
>


In Python 2.5, this is written:

pyfile = 'python.exe' if 'win' in sys.platform else 'python'

However, this is a pretty bad way of doing this at all. sys.executable
is better. I'm not sure when sys.executable was added but I assume
it's more recent than whatever version Lutz used in his book.



More information about the Python-list mailing list