tags:

views:

250

answers:

8

I was going through the code golf question here on Stack overflow and saw many perl one liner solution.

My question is: Is something like that possible in Python?

+12  A: 

python -c 'print("Yes.")'

Daenyth
+7  A: 

It's possible to write one liners in Python but it's awkward (Python encourages well indented code which is somewhat at odds with the "one-liner" concept). It's a bit easier in Python 3 because print() is a function and not a statement. Here's one:

python -c "fact = lambda x: x * fact(x-1) if x > 1 else 1; print(fact(5))"

Here's how you could write a grep like function (this example prints lines from input containing "foo"):

python -c "for s in __import__('fileinput').input(): __import__('sys').stdout.write(s) if 'foo' in s else None"
Greg Hewgill
A: 

Here is my trick to run multiple statements:

[stmt1, stmt2, expr1][2] if requires lazy evaluation: [lambda(): stmt1; lambda(): stmt2][not not boolExpr]()

SHiNKiROU
+2  A: 

In Bourne shell you can use something called heredoc to get around Python's dependency on indents:

s@d400:0:~$ python << 'EOF'
> for i in range(3):
>  print i
> EOF
0
1
2
s@d400:0:~$
S010
no you cant. you indented `print`. Try it without indenting it and you get an `IndentationError` just like you're supposed to.
aaronasterling
I phrased it wrong. This gives you the ability to indent code, without actually having to create a file, open editor, etc.
S010
+2  A: 

I end up wanting to do this fairly often when doing stuff from the shell. It doesn't end up being more compact, and in many cases it's easier to just write a multi-line shell command than to write everything as a lambda. You pretty much can't use any python statement that ends with a colon. So you end up having to

  • write any for-like code as a genexp or list comprehension. I do this anyway for most stuff, but it's annoying to have to import sys and push everything to sys.stdout.writelines in cases where you could otherwise just

    for tree in forest:
        print tree   
    
  • write lambdas instead of function definitions. This is often workable, and has the useful side effect of forcing you to write very directed functions that really only do one thing. However it's not particularly convenient, and doesn't work for anything that mutates a value (e.g. dict.update) and then returns some element.

  • not bother doing things properly with context managers

  • not do any exception handling.

  • Use a dictionary of lambdas instead of any if/else sections.

  • Use type(name, bases, dict) to declare any classes. This is pretty fun but only works if you happen to be declaring a class whose methods can all be expressed as lambdas.

So for some things it works out but generally it's a big hassle, because you end up having to use a functional style that Python doesn't really support. Most of the time I just write multiline shell commands like

who@whoville ~ $ python -c $'
import some_module
for v in some_module.whatever():
    print "Whatever: \'{0}\'".format(v)
'

The $' is a bash quoting syntax, an alternative to its '...' and "..." quoting constructs. It's useful because it works like '...' but lets you escape contained ' characters with \'. You can also embed newlines, so the above code could also be written as python -c $'import some_module\nfor v in some_module.whatever():\n print "Whatever: \'{0}\'".format(v)'. However, this is something of an acquired taste.

One annoying thing about writing multiline commands in bash is that HOME and END go to the beginning of the command rather than the beginning of the line. There may be a better way to do this, but I usually just scan back and forth by holding down CTRL and the left/right arrow keys. Some emacs user could probably set me straight here, since that's where bash's normal keybindings come from.

If you want to insert a line break while editing a multiline command, you can do this with ^V-^J. That will put in a line break in such a way that you can still scan back to the previous lines, rather than using the

$ first line of the command
> second line
> third line

setup that you get otherwise, where you can't get back to the previous lines.

The trick with ^V-^J works in ipython too, making it useful for editing class or function definitions. It may also work in the basic python REPL (probably), I just don't know because I nearly always use ipython.

intuited
A: 

Yes, actually it is very common. I use one liners when I need to write quick code. It just depends on what you want to do. Here is a small line I just used this evening. It is the creation of a Tkinter button in a single line.

a = Button(root, text="Button", command=com1).pack()

If you need any specific code, let me know, I'd be more than happy to help!

Zachary Brown
A: 

A really nice Python one-liner (as in "quite useful"):

python -c 'import SimpleHTTPServer as foo; foo.test()'  23433

Instant basic webserver in the current directory. (Just introduced to this today; very handy.)

Joe McMahon
A: 
exec("if 6 * 9 == int('42', 13):\n\tprint('Yes!')")

With this approach, every Python program can be written as a one-liner :)

dan04