r/Python • • Nov 11 '08

Why the hatred for functional programming?

0 Upvotes

86 comments sorted by

View all comments

5

u/Wiseman1024 Nov 11 '08 edited Nov 11 '08

Okay, I don't think I need to tell why I'm asking this. While Python supports functional programming to a reasonable extent, there's a widespread stubborn hatred of functional programming and anything related to it in the Python community (probably starting with the BDFL), and any proposals for extending Python into something more useful for functional programming (without taking out any of the other ways to use Python, of course, that's the good thing about multi-paradigm languages) is received with ruthless hostility and perceived as an attack against Python.

The Python community is generally a good one (especially compared to others), yet this generalized hatred for functional programming and anything related to it seriously turns me off. While Python is currently my overall favourite programming language (considering the quality of the standard and non-standard libraries, support, and features), I'm currently considering putting my effort and time investments somewhere less hostile to functional programming.

Hostility to functional programming is a form of hostility to abstraction, theoretical correctness and cleanliness. I hope I'm wrong, but this seems like pulling the PHP way. Consistently meeting proposals such as tail call elimination with nothing short of "that's liek difficult to understand d00d ha ha no freaking way" doesn't say much about the health of this community; in fact it looks like something straight out of the average PHP documentation comment.

10

u/DarkGoosey Nov 11 '08

Well I am not really part of the Python community, but I am a redditter and this gives me a bit of exposure I suppose.

From my point of view the biggest issue is the way Function Programming zealots approach the table. There is a pre-existing assumption that everything functional is good and everything imperative is bad. Obviously this is absurd, yet in so many functional-programming articles that flow through reddit there seems to be a subconscious bias toward this line of thinking.

Viewing it from a pragmatic point of view functional programming hasn't really delivered for me. Recently there was a mario-brothers clone written in Haskell (I think) that was hailed as a major achievement. I don't want to be negative towards the authors of that, but it really doesn't seem like a fantastic achievement. In reality it seems to reveal how ill suited purely functional programming is for real world projects.

Of course to swing the other direction and disregard everything functional as academic bluster which is not useful at all in the 'real world' is equally absurd. The fact is that there are a huge amount of things I have taken from functional programming. I program in C++ primarily, although I experiment around with D and it's functional features are the most appealing reason for me to do so.

3

u/arnar Nov 12 '08

You make an excellent point - functional programming isn't the answer to all problems, yet there are tons of useful things to be taken from functional programming.

That's a pretty good reason to incorporate functional ideas in a multi-paradigm language.

5

u/Wiseman1024 Nov 11 '08

While functional programming is undoubtedly purer (which is a criterion of good) and academically correct (which is another criterion of good), and it's more useful for specifying and reasoning over procedures and abstractions, I agree that it's weak for real-world projects (in spite of considering it a superior programming technique).

That's why I want a good multi-paradigm programming language. Build the skeleton, the I/O and (as much as I hate this word) the glue on an imperative framework, then implement the application logic in a nice, provable, correct functional manner, and share a powerful object system such as Python's across them. This is my vision of how a real-world application in Python's domains should be built.

3

u/mycall Nov 11 '08 edited Nov 12 '08

Sounds like you just described F# or Scala

→ More replies (1)

7

u/[deleted] Nov 11 '08

[removed] — view removed comment

9

u/earthboundkid Nov 11 '08

Don't you know? At the Pearly Gates St. Peter's job is to turn away those who can't write a factorial function that doesn't use variables. Being pure is a matter of grave importance for your soul!

→ More replies (1)
→ More replies (4)
→ More replies (2)
→ More replies (10)

6

u/llimllib Nov 11 '08 edited Nov 11 '08

Besides lack of support for tail calls, which is the only example you give, what makes you think that python doesn't support functional programming? What proposals do you refer to that have been shot down?

My POV: Python spports FP just fine as long as it doesn't conflict with python itself. Remember that Python has:

  • list comprehensions

  • generators

  • coroutines

  • filter map and reduce built-in

  • real first-class functions with closures (albeit those can be a bit quirky)

All of those things point to no fundamental language bias against FP.

2

u/Wiseman1024 Nov 11 '08

Besides tail call elimination, which could be added but and it's not being rejected because it can't be done, but because it's deliberately unwanted, I have:

  • Guido's stubborn quest to make lambda useless by not accepting statements inside it, or by not making statements expressions. He even wanted to remove it at one point, but he saw it was pressing too hard.

  • The closures are pretty real, but the way scopes are handled is inconvenient. It could have been fixed in so many numerous ways so long ago, but it was always an nth-priority. The chosen solution, nonlocal, is acceptable but suboptimal. Expanding lambda into a def with better semantics would have been much, much better, and even more backwards-compatible, since it wouldn't require the introduction of a new keyword.

  • The "flat is better than nested" philosophy is keeping the language from becoming more expressive. In particular, it still has statements. They need to die. I've been talking somewhere else in Reddit about how we could have Python be fully backwards-compatible while turning its current statements (a design wart from the age of Fortran) into expressions. There are countless advantages to that, which is basically lifting a limitation, but I've seen the community even wants an arbitrary limitation to stay just so that you cannot write more flexible code.

  • filter, map and reduce? You're pointing out a weak spot, not a strong one. First of all, Guido actively discourages their use in most cases, and he's moving reduce out of builtins (he also expressed his dislike of it many times).

  • More importantly, Python ships with an extensive standard library to handle text in many ways, lots of class utilities, lots of pretty much everything, and almost no FP-related utilities in comparison. The FP utilities have been neglected since ever. Where are promises, futures, foldr, foreach/repeat/other functional loop constructs, scanl/r, flip, prog1, progn, conses, memoizators...?

2

u/[deleted] Nov 12 '08

What you're saying is reasonable, but it sounds like Python isn't your cup of tea, so why don't you use a different language?

I'm not trying to pick a fight, it just sounds like Python pisses you off.

→ More replies (1)

3

u/llimllib Nov 12 '08 edited Nov 12 '08
  • Guido prefers named functions to unnamed ones; it's his language, how does that affect the functionality of the language?

  • The scope handling is what I was referring to as a bit quirky. I can see this as a beef with the language, and might even agree somewhat, but I don't see it in the light of opposition to FP that you do.

  • Again, I cautiously agree while seeing your point as largely orthogonal to your original hostile-to-FP complaint.

  • Who cares if they're in builtins or functools? Or if Guido prefers spelling filter(lambda x,y: something(x,y), collection) as [something(x,y) for x,y in collection]?

  • It definitely doesn't ship with an FP toolkit, and the stdlib doesn't return self on some things that are truly maddening. However, I see that as being because Python is not a primarily functional language, and so the builtins need can reflect that.

However, the language also doesn't do anything to prevent you from adding those. If anything, I think the language should be characterized as cautiously tolerant of FP style, with one big exception, tail calls.

(Furthermore, you're making fine rational arguments (in our thread at the least) and people shouldn't be downvoting you. Raise your level, redditors.)

2

u/Wiseman1024 Nov 12 '08

Guido prefers named functions to unnamed ones; it's his language, how does that affect the functionality of the language?

It doesn't affect the functionality outside namespace pollution, but it affects readability. Wasn't readability a goal of Python?

Who cares if they're in builtins or functools?

Not too important (every of my modules does import * from functools on top), but it shows the focus. You asked if there's a fundamental bias against FP and I was replying to that.

However, I see that as being because Python is not a primarily functional language, and so the builtins need can reflect that.

Python is not primarily a language for writing HTTP servers, yet it ships with one in the standard library. Even if it's not the main focus, FP could be given some attention.

(Furthermore, you're making fine rational arguments (in our thread at the least) and people shouldn't be downvoting you. Raise your level, redditors.)

My grandparent post was seen as criticism on Python, so I guess the fanboys will downvote. Since there are more fanboys than people willing to engage in rational discussion, the total score is going to be negative. Same as for the whole Reddit post.

→ More replies (2)

6

u/sard Nov 11 '08 edited Nov 11 '08

that's the good thing about multi-paradigm languages

I don't think the BDFL is interested in creating a multi-paradigm language. He's much more interested in a 'one obvious consistent Pythonic way of doing everything' language, and that one way historically hasn't been functional.

3

u/Wiseman1024 Nov 11 '08

Then instead of getting the best possible language in its domain, he'll get the most Pythonic language, for an arbitrary and sometimes bizarre definition of Pythonic.

5

u/earthboundkid Nov 11 '08

There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.

3

u/mycall Nov 11 '08

Don't languages that have more than one way to do something appeal to more people and thus service more requirements and applications in the long run?

3

u/earthboundkid Nov 11 '08

No language can be perfect for everyone. I tried to make Ruby perfect for me, but maybe it's not perfect for you. The perfect language for Guido van Rossum is probably Python.

— Matz

→ More replies (2)

2

u/[deleted] Nov 12 '08

Sometimes, languages that have more than one way to do it suck. Perl, for example.

My opinion, but really most of this discussion here has been spiraling downward into the dead-horse-beaten hellhole of language flamewars. Plus, I do appreciate Perl for what it did well and the positive impact it's had on other languages, like Python.

→ More replies (4)

4

u/ubernostrum yes, you can have a pony Nov 11 '08

Well, mostly there seems to be resistance to turning Python into Scheme, because, well, if you like Scheme it's already there for you to use (I'd make a joke here about how a future revision of Scheme should turn non-tail-recursive functions into a compiler error, but I'm afraid somebody would actually think that's a good idea). Oh, and for the record: I'm against tail elimination in Python simply because it seems inevitably to lead to people writing obfuscated, hard-to-read code simply for the sake of having a tail-recursive function, and Python is, well, not about that: it's about clear, readable code.

But as others have pointed out, one of the biggest problems, to me as a Python programmer, is the zeal with which FP folks approach this; it's not a case of "we want the ability to do more FP stuff in Python", it's "FP should be the one and only way in which programmers are allowed to write code". You yourself are leaning that way with your straw-man "hostility to abstraction" swipe (hint: OOP is also abstraction. It's different abstraction than FP, but it's abstraction nonetheless).

Meanwhile I'm quite happy to take advantage of closures, comprehensions, coroutines, function composition and all the other things Python lets me do, without thinking that has to be the only way I think about or write code. So maybe the problem is in yourself and not in Python :)

3

u/arnar Nov 12 '08

I'm against tail elimination in Python simply because it seems inevitably to lead to people writing obfuscated, hard-to-read code simply for the sake of having a tail-recursive function, and Python is, well, not about that: it's about clear, readable code.

That's a pretty weak reason. You can (ab)use many of Python's features to make extremely obfuscated code - such as decorators, metaclasses, context managers etc. etc.

The best reason (although I don't think it is a good one) against tail-call-elimination is confusing stack traces in case of exceptions.

Tail call elimination also actually allows you to write clearer code while still being efficient.

→ More replies (2)
→ More replies (6)

2

u/aieee Nov 11 '08 edited Nov 11 '08

"Hostility to functional programming is a form of hostility to abstraction, theoretical correctness and cleanliness"

I don't think the community or the BDFL are hostile to these things, more generally conservate and hostile to certain coding styles.

The Python way is to prefer easy digestability over things like proper lambdas, and reduce. Lambda is crippled to stop people writing ugly code. Reading the occasional blog post with ugly Ruby code makes it easier to understand why this choice was made.

I would personally prefer more support for functional programming, but I sort of understand the logic of not accepting functional features without carefully judging the trade-offs with the 'pythonic' way.

→ More replies (6)

2

u/nevare Nov 11 '08

Python is also my favorite language and what I miss the most from functionnal programming are

  • tail call optimisation (I think I heared people didn't wanted it ... but maybe it was just never implemented ?)

  • more powerfull lambdas

  • implicit return of the last executed line (it makes you feel you are in a functionnal language)

The last two points are solved in ruby, but they didn't implement tail call optimisation either.

→ More replies (4)

3

u/cc81 Nov 11 '08
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

6

u/kinghajj Nov 11 '08

Beautiful is better than ugly. Simple is better than complex. Flat is better than nested. Readability counts. If the implementation is easy to explain, it may be a good idea.

And functional programming helps with all of these goals, especially if (gasp) you optimize tail calls! Recursion is beautiful.

2

u/Wiseman1024 Nov 11 '08

I know, the Zen of Python.

Beautiful is better than ugly.

Yay, an argument for functional programming.

Explicit is better than implicit.

Yay, an argument against Perl.

Simple is better than complex. Complex is better than complicated.

Yay, unless Guido thinks Fortran is simple and Scheme is complex. If so, I suggest he tries assembly, he'll find it the simplest language ever.

Flat is better than nested.

Bingo. This is a fundamental flaw of Python's philosophy. This is so wrong I don't know where to start. This is an argument against abstraction, against flexibility, against expressivity, and against anything that's good in a programming language. I'm starting to think I should take my previous suggestion seriously, and write Python about the wonderful language assembly which has no nestable elements.

Sparse is better than dense.

More arguments against Perl.

Readability counts.

An argument for functional programming and nesting without requiring you to jump when reading code.

Special cases aren't special enough to break the rules.

Okay, at least he got this right: he doesn't want Perl.

Although practicality beats purity.

Meh. Only to a certain minimal extent. And it's disgusting and shameful when it does. It shouldn't be written here, it may spoil people.

Errors should never pass silently.

Yes, please.

Unless explicitly silenced.

You mean, like PHP's @ ? (j/k)

In the face of ambiguity, refuse the temptation to guess.

Of course. I think I fully agree with Python's syntax philosophy (except for the little bit where less powerful cousins of expressions exist for no reason, serving only as a left-handed hammer since you already had expressions and they were much better).

There should be one-- and preferably only one --obvious way to do it.

I used to think this is a good idea, but I'm seeing it leads to "Pythonic", and "Pythonic" leads to a sub-optimal language.

Although that way may not be obvious at first unless you're Dutch.

I'm not worried if it's not obvious. I'm worried that it's not good.

Now is better than never. Although never is often better than right now.

Agreed.

If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea.

Yes! And if the explanation is the implementation, you've achieved Satori. In other words, you're programming functionally. Now let's pay proper attention to FP, please.

Namespaces are one honking great idea -- let's do more of those!

Yes, but don't lose the focus: it's lexical closures the number one honking great idea.

→ More replies (20)