I cannot evaluate this statement...

W

waltbrad

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' ?
 
J

Jerry Hill

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

This seems to work as expected on my Ubuntu box.

Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import os, sys
sys.platform 'linux2'
pyfile = (sys.platform[:3] == 'win' and 'python.exe') or 'python'
pyfile 'python'

What do you get for sys.platform when you run this code under linux?
 
T

Tim Chase

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@rubbish:~$ uname -a
Linux rubbish 2.6.22-2-686 #1 SMP Fri Aug 31 00:24:01 UTC 2007
i686 GNU/Linux
tim@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
 
C

Chris Mellon

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' ?


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.
 
S

Steven D'Aprano

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

Not on my Linux box.

import os, sys
sys.platform 'linux2'
(sys.platform[:3] == 'win' and 'python.exe') or 'python'
'python'



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

When the first three letters of sys.platform aren't 'win'.
 
J

John Machin

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,348
Latest member
RoscoeNevi

Latest Threads

Top