Live data from Hacker News

Charming Python: Functional programming in Python

ibm.com

1–10 of 26 posts

Re: Charming Python: Functional programming in Python

#4
post #3

why doesn't python have anonymous functions (not just lambdas which can only be expressions)?

Priorities and style. First-class functions are available, and a lot of veteran Python programmers, Guido included, don't like an overwhelmingly functional style.

It's considered opaque.

Personally, I like to fiddle with Clojure in my free time.

Re: Charming Python: Functional programming in Python

#6
post #2

Ever since I've dabbled a bit with Lisp my Python (and javascript) code has become much more functional-y. Still not sure whether this is a good or bad thing.

Its funny you mention that. Ive had the same experience with all my code. I found my C++, Java and Python all become more functional thanks to playing around with haskell!

Re: Charming Python: Functional programming in Python

#9
post #2

Ever since I've dabbled a bit with Lisp my Python (and javascript) code has become much more functional-y. Still not sure whether this is a good or bad thing.

I think it's a double-edged sword.

I've both written and read code in non-functional languages where the "easy" way of doing it is thrown aside for the "cool functional" way of doing it. Leading to code which was more fun to write, looks cooler, but is also hard to read and written in a different idiom to the actual programming language. Bad.

I'm 100% guilty of this, although I try to stay vigilant. The other day I refactored out a gnarly bit of a Python API, but caught myself feeling sad because it meant deleting a bunch of clever lambda functions I was using to work around it in the first place. Bad!

That said, the other side of the double-edged sword is great - things that lead to giant tangled messes of procedural code can be turned into very nice functional-style constructs. Also, I love having list comprehensions in Python.

Re: Charming Python: Functional programming in Python

#10
post #8
post #3

why doesn't python have anonymous functions (not just lambdas which can only be expressions)?

It lets you define functions on the fly and use them, so the only difference from anonymous functions is that you have to pick a name.

Yeah, I almost wish they didn't use the keyword 'lambda' at all. If you could just write:

    def foo(x):
        def lambda():
          print x
        lambda;bar(callback=lambda);baz(42,lambda)
        def lambda():
          print x * x
        lambda;bar(callback=lambda);baz(42,lambda)
It'd stop this silly complaint. I just end up using 'def lambduh'

Slightly annoying that you need to define the function on the line before, can't inline it, but not nearly as bad as people make it out to be.

Post reply on HN