do python's nifty indentation rules spell the death of one-liners?

Peter Hansen peter at engcorp.com
Tue Apr 15 20:50:11 EDT 2003


Dan Jacobson wrote:
> 
> I need to use one liners every day, especially in Makefiles.  I thus
> need to know how to make any kind of python program into a big long one
> liner. However, right out of the starting gates I run into
> $ python -c 'print 2;for i in (1,4):print i'
>   File "<string>", line 1
>     print 2;for i in (1,4):print i
>               ^
> SyntaxError: invalid syntax
> 
> Help. BTW tell me what to do in all the other cases when ";" can't be
> a replacement for newline.  I.e. does python's nifty indentation rules
> spell the death of one-liners?
> 
> BTW, how do I import a module from the command line when using -c? Is
> there an option switch for importing modules?
> 
> This makes no output and returns no error value to the shell:
> $ python -c 'import math' -c 'for i in range(200,500,50): print
>   i,360/2/math.pi*math.asin(i/1238.4)'

I think you're looking for this (all on one line, with a space instead
of the line break):

C:\>python -c "exec '''import math\nfor i in range(200,500,50):\n print 
i,360/2/math.pi*math.asin(i/1238.4)\n'''"

200 9.29389730029
250 11.6465309088
300 14.0192608728
350 16.4168005237
400 18.8442931045
450 21.3074328157

-Peter




More information about the Python-list mailing list