Usando un condicional en el comando python interpreter -c

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'
    print '32'


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

preguntado el 31 de julio de 12 a las 12:07

3 Respuestas

After a (very) brief search, I can't find this documented anywhere, but it seems that -c strictly takes an expresión (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 el función de impresión (function calls are an expression) rather than the print statement
  2. Utilice Python a if b else c expresión condicional

Esto te da:

lvc@tiamat:~$ python -c "from __future__ import print_function; import sys; print('64' if sys.maxsize > 2**32 else '32')"

Respondido 31 Jul 12, 12:07

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

@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, . But as I said in the answer, think of 'expression' as 'anything you can put on the RHS of ='. - lvc

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'

Ver también: ¿Python tiene un operador condicional ternario?

contestado el 23 de mayo de 17 a las 13:05

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

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

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

To get all platform info you could:

$ python -mplatform

Or just an architecture:

$ python -c "import platform; print platform.architecture()"
('64bit', 'ELF')

Respondido 31 Jul 12, 12:07

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. Ver este post - Kevinmm

es un error; 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

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.