Live data from Hacker News

10 Python One-liners To Impress Your Friends

codeblog.dhananjaynene.com

11–20 of 20 posts

Re: 10 Python One-liners To Impress Your Friends

#13
post #9

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.

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

#18
post #7
post #3

Why is number 1 better than just: [x*2 for x in range(1, 11)]

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.

or just:

    set(tweet.split()) & set(wordlist)

Re: 10 Python One-liners To Impress Your Friends

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

Post reply on HN