Live data from Hacker News

Unpythonic Python

skien.cc

71–80 of 156 posts

Re: Unpythonic Python

#72

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

[deleted]

Re: Unpythonic Python

#73

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

[deleted]

Re: Unpythonic Python

#74

The superfluous semicolons on line endings in the C example gave me a chuckle.

Heh, in the C version I wish that "value = str(i);" was replaced with "value = "%d" % (i);"

Format strings feel like C. And not the fun parts of C.

Re: Unpythonic Python

#75

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)]

This way's easier to read:

    ['FizzBuzz' if 0 == n % 15 else 'Fizz' if 0 == n % 3
        else 'Buzz' if 0 == n % 5 else str(n) for n in range(1,101)]

Re: Unpythonic Python

#76
There's no honest attempt to explain the philosophy behind every language with real-world examples in Python. It's just childish jokes.

It's the equivalent of saying "Ze fish in le river" is "Frenchish English". Ha ha, stupid French people, right?

It's easy to make fun of programming languages like this, but what does that teach us? Aside from to mock what is different than what we use right now?

Re: Unpythonic Python

#77

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.

So what's if it's shorter. You'll run out of screen? You'll wear down your keyboard? If you don't like typing it, have a macro. Otherwise, as Objective C shows, the verbosity of code is not relevant at all. It's what it says and what it does, which is.

You realise the equivilent Objective-C syntax is ^{} right?

And access modifiers are + and - rather than static and nonstatic.

Objective-C definitely isn't above using lightweight syntax. Where it favours verbosity is APIs.

Re: Unpythonic Python

#79
There's something minor that's always bothered me about the usual description of FizzBuzz:

> If the number is divisible by 3, print Fizz instead of the number. If it’s divisible by 5, print Buzz. If it’s divisible by both 3 and 5, print FizzBuzz.

In a strict interpretation, the first two sentences could be seen as incorrect. If a number is divisible by 3, you cannot just print 'Fizz' and move on. You also have to check if it's divisible by 5.

Re: Unpythonic Python

#80

Earlier quoted context omitted.

So what's if it's shorter. You'll run out of screen? You'll wear down your keyboard? If you don't like typing it, have a macro. Otherwise, as Objective C shows, the verbosity of code is not relevant at all. It's what it says and what it does, which is.

You realise the equivilent Objective-C syntax is ^{} right? And access modifiers are + and - rather than static and nonstatic. Objective-C definitely isn't above using lightweight syntax. Where it favours verbosity is APIs.

I realize it, right.

But I hoped you'd see beyond the direct comparison of lambdas with Objective C's blocks.

I'm saying that short code doesn't automatically translate to more readable or better code, Objective C's message syntax is the proof (if I have to be painfully specific about it).

I write a lot of JS and typing out "function" or "return" was never on the list of things I found a problem, despite I also work in C#, which uses the short => variation.

Post reply on HN