6

I am trying to figure out how to pass the following conditional statement into the python interpreter's command option (-c).

if sys.maxsize > 2**32:
    print '64'
else:
    print '32'

64

However, I continually get syntax errors, such as the following:

>python -c "import sys; if sys.maxsize > 2**32: print '64' else: print '32';"
  File "<string>", line 1
    import sys; if sys.maxsize > 2**32: print '64' else: print '32';
                 ^
SyntaxError: invalid syntax

I found it surprisingly difficult to find a good example of this usage. I must be missing something big here...

kevinmm
  • 3,136
  • 4
  • 21
  • 24

3 Answers3

5

After a (very) brief search, I can't find this documented anywhere, but it seems that -c strictly takes an expression (ie, something that can appear on the RHS of an assignment), not a statement. To get around this in your case, you need to do two things:

  1. Use the print function (function calls are an expression) rather than the print statement
  2. Use Python's a if b else c conditional expression

This gives you:

lvc@tiamat:~$ python -c "from __future__ import print_function; import sys; print('64' if sys.maxsize > 2**32 else '32')"
64
lvc
  • 34,233
  • 10
  • 73
  • 98
  • I think you meant else '32', but this is great. So, statement is equivalent to expression? From the Python Docs: `When called with -c command, it executes the Python statement(s) given as command. Here command may contain multiple statements separated by newlines. Leading whitespace is significant in Python statements!` – kevinmm Jul 31 '12 at 11:30
  • @DevHopeful_2012 I did mean `else '32'`; fixed now. That part of the man page seems to be saying that `-c` takes anything you can put at the interpreter prompt, which I can't reconcile with what I'm actually seeing. 'Statement' is a superset of 'expression' in Python - see, eg, http://docs.python.org/reference/expressions.html . But as I said in the answer, think of 'expression' as 'anything you can put on the RHS of `=`'. – lvc Jul 31 '12 at 11:36
4

The existing two answers are better, but here's an alternative (tested with Python 2.7):

> python -c 'import sys; print 64 if sys.maxsize > 2**32 else 32'
64

See also: Does Python have a ternary conditional operator?

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 2
    Hmm, I thought it would look something more like this. I believe your answer is just as elegant, and my requirement will only be for Python 2.7 or greater. So, this would definitely work, as well. However, lvc beat you to the draw and has clarified my misunderstanding. Thanks for the help anyways! – kevinmm Jul 31 '12 at 11:52
1

You could use multiple lines (at least in bash):

$ python -c "import sys
> if sys.maxsize > 2**32:
>  print '64'
> else:
>  print '32'"
64

To get all platform info you could:

$ python -mplatform

Or just an architecture:

$ python -c "import platform; print platform.architecture()"
('64bit', 'ELF')
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • With architecture, don't you still have to be careful about OSX, where the interpreter can be run as a different bitness? I am hazy on the details, but I saw several comments that platform.architecture() cannot be trusted. [See this post](http://stackoverflow.com/a/1842699/995617) – kevinmm Jul 31 '12 at 11:44
  • it is a [bug](http://bugs.python.org/issue10735); `sys.maxsize` seems like a valid workaround (I don't know whether there is a system where python works but `sizeof size_t != sizeof pointer`). – jfs Jul 31 '12 at 12:14