Unpythonic Python
skien.cc
Unpythonic Python
1–10 of 156 posts
Re: Unpythonic Python
#2Re: Unpythonic Python
#3https://gist.github.com/tghw/3702360
Definitely a little unpythonic.
Re: Unpythonic Python
#4Re: Unpythonic Python
#5Playing code golf with Python, this is the shortest I could get: https://gist.github.com/tghw/3702360 Definitely a little unpythonic.
for x in range(100):
print x%3/2*'Fizz'+x%5/4*'Buzz' or x+1Re: Unpythonic Python
#6Neat! 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.
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, xrange(1, 101))
It's pleasing to use HFOs in Python, as long as you don't abuse lambdas. Also, some functional types like `defaultdict` can be used to describe code/business logic with datastructures rather than a bunch of if's, keeping things tidy.Re: Unpythonic Python
#7Re: Unpythonic Python
#8Playing code golf with Python, this is the shortest I could get: https://gist.github.com/tghw/3702360 Definitely a little unpythonic.
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.Re: Unpythonic Python
#9Re: Unpythonic Python
#10Playing 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.
It's not: with def() you have to explicitly write return, while with lambda it is implicit.