Live data from Hacker News

Unpythonic Python

skien.cc

61–70 of 156 posts

Re: Unpythonic Python

#61
post #44
post #40

When you write too much Haskell, your Python code starts to look like this: print('\n'.join( 'FizzBuzz' if x%5==0 and x%3==0 else 'Fizz' if x%3==0 else 'Buzz' if x%5==0 else str(x) for x in range(1, 101))) I would really like to have a "let" expression in Python to avoid having to write a new function with a def statement when you could get away with a simple lambda or generator expression.

"let = lambda" would be the same thing, wouldn't it?

    >>> let = lambda
      File "", line 1
        let = lambda
               ^
    SyntaxError: invalid syntax
Am I misunderstanding you?

Re: Unpythonic Python

#62
post #25

Earlier quoted context omitted.

That's just horrible. I don't know what more to say about it than that. How did anyone think this would be a good idea on balance?

Mmm I suppose it's really just preference. I love that syntax. But I guess there's no arguing about taste.

Well, it's typing-friendly but not (re)learning-friendly. I guess it depends how you weigh those concerns.

Re: Unpythonic Python

#64

One of the solutions in the comments I found quite pythonic and concise. Somehow people have it in their heads that "Pythonic" means long-winded. And yes, you have to read the code and think for a second to understand it, but that's no crime. [(not x % 3) * 'Fizz' + (not x % 5) * 'Buzz' or x for x in range(1, 101)]

Very nice.

If you make the generator always return a string, then you can make it print, thus:

    print '\n'.join((not x % 3) * 'Fizz' + (not x % 5) * 'Buzz' or str(x) for x in range(1, 101))

Re: Unpythonic Python

#65
post #61
post #44

Earlier quoted context omitted.

"let = lambda" would be the same thing, wouldn't it?

>>> let = lambda File " ", line 1 let = lambda ^ SyntaxError: invalid syntax Am I misunderstanding you?

>>> let = lambda x: 2*x

>>> print let(4)

8

Edit: Awesome, downvoted for helping.

Re: Unpythonic Python

#67
post #3

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

Here's mine: ['Fizz' * (not bool(i % 3)) + 'Buzz' * (not bool(i % 5)) for i in xrange(1,101) if i % 3 == 0 or i % 5==0] EDIT: I hate lambdas. Also I'm not saying the above is pretty or a good idea, but I had some fun.

Mine with some itertools abuse :)

from itertools import cycle , izip

[(f + b or x) for f, b, x in izip(cycle([''] * 2 + ['Fizz']), cycle([''] * 4 + ['Buzz']), range(1, 100))]

Edit: not sure how to post code in comments. e.g. * is lost

Re: Unpythonic Python

#69

Earlier quoted context omitted.

Here's mine: ['Fizz' * (not bool(i % 3)) + 'Buzz' * (not bool(i % 5)) for i in xrange(1,101) if i % 3 == 0 or i % 5==0] EDIT: I hate lambdas. Also I'm not saying the above is pretty or a good idea, but I had some fun.

Mine with some itertools abuse :) from itertools import cycle , izip [(f + b or x) for f, b, x in izip(cycle([''] * 2 + ['Fizz']), cycle([''] * 4 + ['Buzz']), range(1, 100))] Edit: not sure how to post code in comments. e.g. * is lost

Nicely done.

> Edit: not sure how to post code in comments. e.g. * is lost

Put a couple of blanks before each line. This displays it in a monospace font, too.

Re: Unpythonic Python

#70
post #60

One of the solutions in the comments I found quite pythonic and concise. Somehow people have it in their heads that "Pythonic" means long-winded. And yes, you have to read the code and think for a second to understand it, but that's no crime. [(not x % 3) * 'Fizz' + (not x % 5) * 'Buzz' or x for x in range(1, 101)]

Things taking longer to read and understand is perhaps the central crime of unmaintainable code, no matter how concise otherwise.

To quote Brian Kernighan, "Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?"
Post reply on HN