Live data from Hacker News

Unpythonic Python

skien.cc

11–20 of 156 posts

Re: Unpythonic Python

#11
post #3

Playing code golf with Python, this is the shortest I could get: https://gist.github.com/tghw/3702360 Definitely a little unpythonic.

Something annoying about python is that the word "lambda" is so long for what are supposed to be one-off functions. You have f=lambda x,y=1,a='Fizz',b='Buzz': ... But it's actually shorter to say def f(x,y=1,a='Fizz',b='Buzz'): ... I much prefer Haskell's \x -> x Style lambdas.

For python golf, exec and string multiplication is usually a good strategy:

https://twitter.com/jooon/status/25054586175

But that isn't a whole lot shorter than the solution pcmonk posted in the thread.

Re: Unpythonic Python

#12

Neat! I'd be interested in seeing pythonic solutions written in other languages (to the extent that those languages may allow 'Pythonic' style), too. I find it fascinating to see how certain languages will, for one reason or another, trend towards certain design patterns and styles.

Actually, most Python I write (influence from reading experienced programmers) tends to the last one, although it's semi-jokingly: def fizzbuzz(n): return 'FizzBuzz' if n % 3 == 0 and n % 5 == 0 else None def fizz(n): return 'Fizz' if n % 3 == 0 else None def buzz(n): return 'Buzz' if n % 5 == 0 else None def fizz_andor_maybenot_buzz(n): print fizzbuzz(n) or fizz(n) or buzz(n) or str(n) map(fizz_andor_maybenot_buzz,…

I use defaultdict all the time. Very often when describing graphs.

  g = defaultdict(dict)
Allows you to do

  g[node1][node2] = edge_weight
without checking if node1 exists, and if not, saying g[node1] = {}

Also a neat trick (of dubious use) is:

  def auto_tree(): return defaultdict(auto_tree)
Gives you infinitely nested defaultdicts.

Re: Unpythonic Python

#13
post #10

Earlier quoted context omitted.

Something annoying about python is that the word "lambda" is so long for what are supposed to be one-off functions. You have f=lambda x,y=1,a='Fizz',b='Buzz': ... But it's actually shorter to say def f(x,y=1,a='Fizz',b='Buzz'): ... I much prefer Haskell's \x -> x Style lambdas.

> But it's actually shorter It's not: with def() you have to explicitly write return , while with lambda it is implicit.

You don't need a return, just a print (since we are doing FizzBuzz).

For tghw's solution to work you'd have to have a print outside of the function call (since you can't print from a lambda). With the def, you can print inside of the function (and don't need a return, since you're just printing).

Re: Unpythonic Python

#14
post #3

Playing code golf with Python, this is the shortest I could get: https://gist.github.com/tghw/3702360 Definitely a little unpythonic.

Something annoying about python is that the word "lambda" is so long for what are supposed to be one-off functions. You have f=lambda x,y=1,a='Fizz',b='Buzz': ... But it's actually shorter to say def f(x,y=1,a='Fizz',b='Buzz'): ... I much prefer Haskell's \x -> x Style lambdas.

I generally don't particularly like giving punctuation meaning that, well, isn't punctuating. The bottleneck when writing code is almost never keypresses.

I think CoffeeScript gets it probably the most right of languages I've worked with, in that the choice of punctuation makes intuitive sense... "Hey! It's an Arrow. It goes from here to here".

FWIW, I don't like the word "lambda" either... Naming things with one letter are generally a bad idea that we'd do better to divest ourselves of, but there is a bit of legacy to deal with. Either way, they make a field (and statistics is a major offender here, with p-values and t-tests and whatnot), terribly inaccessible.

Re: Unpythonic Python

#15
We used to have ADA-TRAN code at work. Fortran code migrated to Ada. It was awful, but it worked so we left it.

It been commented the Perl I write, it written like C programmer.

Re: Unpythonic Python

#17
post #14

Earlier quoted context omitted.

Something annoying about python is that the word "lambda" is so long for what are supposed to be one-off functions. You have f=lambda x,y=1,a='Fizz',b='Buzz': ... But it's actually shorter to say def f(x,y=1,a='Fizz',b='Buzz'): ... I much prefer Haskell's \x -> x Style lambdas.

I generally don't particularly like giving punctuation meaning that, well, isn't punctuating. The bottleneck when writing code is almost never keypresses. I think CoffeeScript gets it probably the most right of languages I've worked with, in that the choice of punctuation makes intuitive sense... "Hey! It's an Arrow. It goes from here to here". FWIW, I don't like the word "lambda" either... Naming things with one let…

The reasoning behind using '\' for lambda in Haskell was because it sort of looks like a lambda character ( λ vs \ ) if you squint hard enough.

Re: Unpythonic Python

#18
post #10

Earlier quoted context omitted.

> But it's actually shorter It's not: with def() you have to explicitly write return , while with lambda it is implicit.

You don't need a return, just a print (since we are doing FizzBuzz). For tghw's solution to work you'd have to have a print outside of the function call (since you can't print from a lambda). With the def, you can print inside of the function (and don't need a return, since you're just printing).

A minor quibble, but in Python 3 you can print in a lambda because print is no longer a keyword.

Re: Unpythonic Python

#19
Java doesn't have to be written like that you know. Idiomatic code is overrated in my opinion. Make it readable and correct, fuck the rest.

Re: Unpythonic Python

#20
post #18

Earlier quoted context omitted.

You don't need a return, just a print (since we are doing FizzBuzz). For tghw's solution to work you'd have to have a print outside of the function call (since you can't print from a lambda). With the def, you can print inside of the function (and don't need a return, since you're just printing).

A minor quibble, but in Python 3 you can print in a lambda because print is no longer a keyword.

As a non-Python 3 user, I just opened up the python 3 REPL to try something I always find myself wanting to do in tiny little python scripts:

  map(print, some_list)
Unfortunately, it returned a map object, which I guess is also new in Python 3 (I assume it's a lazy application device).
Post reply on HN