News for hackers. Stuff that matters.
Re:10 Python One-liners To Impress Your Friends by sylvinus on Monday May 28, 2012 @04:26AM (#4032477)
I find it weird that the author writes about one-liners in python without knowing about list comprehensions
Re:10 Python One-liners To Impress Your Friends by jchiu1106 on Wednesday May 30, 2012 @12:21PM (#4043458)
Agreed. Also in his comment he said list comprehension can replace reduce...
Re:10 Python One-liners To Impress Your Friends by sk3tch on Monday May 28, 2012 @04:34AM (#4032492)
10 Python one-liners to piss off your colleagues by ignoring the Zen.

Seriously though, some of these are ridiculously unreadable so think twice before using any for anything more than theory.

Re:10 Python One-liners To Impress Your Friends by rytis on Monday May 28, 2012 @04:43AM (#4032511)
Yet the most useful oneliner is missing:

    python -m SimpleHTTPServer
Re:10 Python One-liners To Impress Your Friends by Tmmrn on Monday May 28, 2012 @07:30AM (#4032895)
And

    python3 -m http.server
Re:10 Python One-liners To Impress Your Friends by sylvinus on Monday May 28, 2012 @10:50AM (#4033581)
That could actually be the best incentive yet to make me install python3 right now :)
Re:10 Python One-liners To Impress Your Friends by Cogito on Monday May 28, 2012 @09:02PM (#4035525)
Number 5 should read (not that it matters): print map(lambda x: "Happy Birthday " + ("to you" if x != 2 else "dear Name"),range(4))

In general, I was expecting more. I was hoping for some slightly more obscure examples, though I guess the point is that many common problems are 1 liners in Python.

Re:10 Python One-liners To Impress Your Friends by FaddiCat on Monday May 28, 2012 @04:25AM (#4032476)
Why is number 1 better than just:

    [x*2 for x in range(1, 11)]
Re:10 Python One-liners To Impress Your Friends by eieio on Monday May 28, 2012 @04:39AM (#4032503)
I felt the same about number 3.

  [x in tweet.split() for x in wordlist]
Seems better to me than

  map(lambda x: x in tweet.split(),wordlist)
Although the natural thing for me to do would be

  [x for x in wordlist if x in tweet]
Which both generates a list of words instead of booleans(['scala', 'sbt'] instead of [True, False, False, False, False]) and also matches the 'sbt' in tweet.
Re:10 Python One-liners To Impress Your Friends by saintfiends on Monday May 28, 2012 @04:07PM (#4034748)
or just:

    set(tweet.split()) & set(wordlist)
Re:10 Python One-liners To Impress Your Friends by shoo on Monday May 28, 2012 @04:51AM (#4032529)
Or with numpy:

    2 * numpy.arange(1, 11)
The usual notation for working with elements of vector spaces is nice when working with arrays of numbers.
Re:10 Python One-liners To Impress Your Friends by antonios on Monday May 28, 2012 @04:26AM (#4032478)
It isn't.
Re:10 Python One-liners To Impress Your Friends by evanlong on Monday May 28, 2012 @04:40AM (#4032506)
sigh ... when will people learn ... one-liners are not good. Especially when they lead to bugs. For example #4 in the list. If you that didn't jump out to you as a bug. You are a fucking idiot and shouldn't be writing production code.
Re:10 Python One-liners To Impress Your Friends by hythloday on Monday May 28, 2012 @04:51AM (#4032530)
For reference, a correct #4 would read:

  with open("one_liners.py") as f: print f.readlines()
In CPython, a file handle is closed immediately the file goes out of scope, because it's reference-counted. In Jython (and I believe IronPython too), with lazier garbage collection, the filehandle would stay open until it was gc'd. I have no idea about PyPy's behaviour.
Re:10 Python One-liners To Impress Your Friends by __alexs on Monday May 28, 2012 @04:14AM (#4032454)
Re:10 Python One-liners To Impress Your Friends by brass9 on Monday May 28, 2012 @04:48AM (#4032524)
"Error establishing a database connection"

Very funny...

Dumb indian script kiddies... sigh

Re:10 Python One-liners To Impress Your Friends by kang on Monday May 28, 2012 @11:24AM (#4033741)
thats racist
Re:10 Python One-liners To Impress Your Friends by darkf on Monday May 28, 2012 @04:40AM (#4032505)
These seem ridiculously mundane.