I cannot evaluate this statement...

Tim Chase python.list at tim.thechases.com
Fri Mar 7 16:04:51 EST 2008


> import os, sys
> pyfile = (sys.platform[:3] == 'win' and 'python.exe') or 'python'
> 
> Okay, run on a win32 machine, pyfile evaluates to python.exe
[snip]
> Now. Run this on linux. The first condition evaluates sys.platform[:3]
> == 'win' as false.
[snip]
> Where am I going wrong.  And when will this statment make pyfile
> evaluate to 'python' ?

Your reasoning is correct.  I'm guessing you're typing something 
wrong?  Or typing the right thing in the wrong window (so that 
the command is run on a Windows box)?  Or perhaps you're running 
on some weird build of Python?  It does indeed work on my Debian box:

tim at rubbish:~$ uname -a
Linux rubbish 2.6.22-2-686 #1 SMP Fri Aug 31 00:24:01 UTC 2007 
i686 GNU/Linux
tim at rubbish:~$ python
Python 2.4.4 (#2, Jan  3 2008, 13:36:28)
[GCC 4.2.3 20071123 (prerelease) (Debian 4.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more 
information.
 >>> import sys
 >>> sys.platform
'linux2'
 >>> sys.platform[:3]=="win" and "python.exe" or "python"
'python'
 >>> (sys.platform[:3]=="win" and "python.exe") or "python"
'python'


Whereas on my Windows machine:

c:\> python
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more 
information.
 >>> import sys
 >>> sys.platform
'win32'
 >>> sys.platform[:3] == "win" and "python.exe" or "python"
'python.exe'

That's both with and without the parens.  Same happens in 
assignments.

-tkc





More information about the Python-list mailing list