It's worth it, if you have to type the boilerplate code hundreds of times. Consider the time wasted in debugging errors due to typos etc. It's even more worth if that shell script (or, in my case an emacs yasnippet) is parameterized.
The factorial itself is only defined for non-negative integers. This may seem pedantic, but what you've defined there is actually Gauss's pi function, which has many interesting mathematical properties, but is not equivalent to the factorial because it has a larger domain. The results that it gives for non-integer arguments aren't any more "correct" than a factorial function throwing an exception, because they're both behaving appropriately for their defined domain.
Edit: The math module has a factorial() function anyways, so it's a bit of a moot point. Use that :)
Edit Edit: Thanks for the catch, I did mean non-negative integers instead of positive.
True, but it requires 3.2+ and does not work correctly for large integers (floating-point runs out of precision long before Python's arbitrary-size integers become unusably large.)
while I know this is a joke I would argue that this is in no way hackerish, this is an asshole version. Basically a middle finger to anyone who wants to work with your code. Also anything but pythonic. (please note this not meant to be an insult to the parent commenter)
It is hackerish to the extent it demonstrates a knowledge of python's internals, however maybe it would better be described as being written by a former assembly programmer. Also, you might be interested in the fact that Paul Graham argues that his dream language would have inline bytecode. It probably wouldn't be written as a list of integers though.
As for this not being pythonic, at least its not self modifying or anything fun. I did once see a talk on obsfucated python that used decorators to implement a Turing complete language, so this is hardly the worst abuse of python ever.
I prefer "short but to the point". This is instinctively what I threw in iPython before looking past first snippet:
print reduce(lambda x, y: x*y, xrange(2, 6+1))
As a newish Python guy (5 months), I'm interested as to why the preferable solution seems to be to import operator and use the multiplication function? (I'm purposely ignoring the more preferable call to the C library)
1) The Python community doesn't really like lambdas. It's almost always considered better style to declare a named function, even if it's a short one-liner. If it's already available in the standard library, use that instead of re-implementing it as an unnecessary lambda.
2) This is an adaptation of jokes that have been made about many languages before Python, so the code isn't very Pythonic to start with. Most of the complex ones have errors that prevent them from running, they're just for comedic effect.
related educational stuff: here's three different ways to do lazy sequences (infinite seqs) in python: generators, closures, and classes. provides implementations of lazy-map and lazy-filter for each style. (the generator implementations are equivalent to those in itertools.) uses fib instead of fac. https://github.com/dustingetz/sandbox/blob/master/etc/lazy.p...
we can use these ideas to elegantly solve the second greplin challenge question: "find the smallest prime fibonacci, X, greater than 227,000; compute sum of prime divisors of X+1"
pred = lambda x: x > 227000 and is_prime(x)
X = take(lazy_filter(pred, fib_gen()), 1)[0]
print sum(filter(is_prime, divisors(X+1)))
I've had the reverse experience. My exposure to programming language libraries and American media has resulted in me accidentally saying "math" sometimes.
Not surprising - many Python built-ins are implemented in C. And for any numerical or computational libraries, doing it in C is all but essential.
Tangential, but this is why NumPy totally changed my workflow. I can use all of the benefits of Python, including its libraries and syntax, and still have a program that executes at the speed of C, rather than Python[1].
[1] Not literally the speed of C, but you get the idea.
http://news.ycombinator.com/item?id=1087068