5 ms·
If you code in Python, then code like a Pythonist would code. Don't code like you would code in Scheme or like you would code in Java. Why? It produces some pre
by amix 18y ago
If you code in Python, then code like a Pythonist would code. Don't code like you would code in Scheme or like you would code in Java. Why? It produces some pretty hard to read code - there's nothing worse than reading some Python code that is written like Java or some other language X. And some languages are optimized for certain things and function calls in Python are pretty expensive...
A little tip: tail_rec is a decorator and it would have been nice if the decorator syntax was used: http://www.python.org/dev/peps/pep-0318/ http://www.python.org/dev/peps/pep-0318/
- deleted 18y ago[deleted]
- paulgb 18y agoI agree that coding in the style of the language is important, and I can assure you that my python code has gotten a fair bit more pythonic since writing this :). Still, there are times when things like this could be useful. Even though I code in python more than anything these days, I still find it useful to "think in scheme" sometimes, and having access to unlimited tail recursion helps. A fair bit of my code is experimental or write-once run-once, so as long as I can understand it performance and readability is not a huge issue. > A little tip: tail_rec is a decorator [...] Unfortunately, it's not that easy. The tail-recursive function itself needs to be able to reference itself (unmodified by a decorator) when it creates the lambda. There is a way around this (by having the function take a copy of itself as an argument, as in the y-combinator), but that has a negative net effect on readability.
- benhoyt 18y agoGood point. To be fair, however, he's probably just being intellectually curious -- that is, "hacking". There's no way you'd test a number for evenness in real life by either recursion or iteration. You'd always do either (n&1)==0 or n%2==0.