I cannot evaluate this statement...

Discussion in 'Python' started by waltbrad, Mar 7, 2008.

  1. waltbrad

    waltbrad Guest

    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' ?
     
    waltbrad, Mar 7, 2008
    #1
    1. Advertising

  2. waltbrad

    Jerry Hill Guest

    On Fri, Mar 7, 2008 at 3:38 PM, waltbrad <> wrote:
    > 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?

    --
    Jerry
     
    Jerry Hill, Mar 7, 2008
    #2
    1. Advertising

  3. waltbrad

    Tim Chase Guest

    > 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
     
    Tim Chase, Mar 7, 2008
    #3
  4. waltbrad

    Chris Mellon Guest

    On Fri, Mar 7, 2008 at 2:38 PM, waltbrad <> 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.
     
    Chris Mellon, Mar 7, 2008
    #4
  5. On Fri, 07 Mar 2008 12:38:11 -0800, waltbrad 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


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




    --
    Steven
     
    Steven D'Aprano, Mar 7, 2008
    #5
  6. waltbrad

    John Machin Guest

    On Mar 8, 7:38 am, waltbrad <> 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' ?
     
    John Machin, Mar 7, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Florence
    Replies:
    3
    Views:
    6,812
    Ken Cox [Microsoft MVP]
    Jun 25, 2006
  2. Danny
    Replies:
    0
    Views:
    575
    Danny
    Nov 25, 2006
  3. No Such Luck

    Evaluate string expression in if statement?

    No Such Luck, Dec 7, 2005, in forum: C Programming
    Replies:
    8
    Views:
    398
    Richard Heathfield
    Dec 8, 2005
  4. Replies:
    2
    Views:
    505
    ondra.holub
    Nov 14, 2006
  5. Steve
    Replies:
    5
    Views:
    183
Loading...

Share This Page