5 ms·
Is Python fully capable and suitable for functional programming?
by bhnmmhmd 9y ago
Is Python fully capable and suitable for functional programming?
- Walkman 9y agoNo, not really :D
- BerislavLopac 9y agoNo. It has a lot of functional support, but it's not ideal if you want to write clean, exclusively functional code. What it is amazing at is mixing the paradigms, using whatever styles fit your immediate need the best.
- an_endofunctor 9y agoIt depends what you need. It obviously has mutable state and loose typing but it has first class functions and you don't need to use classes so I would say yes it is capaable but I wouldn't pick it for a functional language.
- heisenzombie 9y agoIt's quite possible to write code in a functional style in Python. Map pure lambdas over lazy, infinite sequences all you want. However, Python won't stop you writing code with spaghetti-like flow control and rampant side effects. Self-discipline is required. If you want to write only functional code, then you're definitely better off with a more rigid language that explicitly disallows mutation and side-effects. However, if you subscribe to the idea that many real-world programs are naturally expressed as "a functional core inside an imperative shell"^1, then Python's stylistic flexibility might make it a good, practical choice. 1. https://gist.github.com/kbilsted/abdc017858cad68c3e7926b03646554e https://gist.github.com/kbilsted/abdc017858cad68c3e7926b0364...
- yen223 9y agoYou can do functional programming in Python, but it's not pleasant. Python lacks a lot of functional niceties - first-class composition, first-class immutable data structures, tail-call optimisation and multiline lambdas
- xapata 9y agoOn the contrary, I find it quite pleasant despite (or because of) those facts. Multi-line lambdas I'm particularly glad to be rid of.
- polotics 9y agoDefine "fully" and/or... well judge for yourself: https://github.com/dbrattli/oslash/wiki/Functors,-Applicatives,-And-Monads-In-Pictures https://github.com/dbrattli/oslash/wiki/Functors,-Applicativ...
- xapata 9y agoYes. A function is an object just like any other. What more do you need? If you say macros, I'd argue that's not quite the same as "functional".
- Waterluvian 9y agoLots of easy handling of structures immutably. Which Python has a good bit of for sure, with comprehension especially. But I could use more. :) I wish everything immutablejs gave me was built in to Python. :)
- xapata 9y agoComparing with immutablejs, there are 4 broad categories that match: lists, dicts, sets, and iterators. Tuple slicing and concatenation gives you the "immutable" behavior for ordered data. For sets and dicts, one could create a subclass that override add/insert/set methods to return a modified copy. Iterators already have excellent support. I'm not an immutablejs user, but my first skim of the docs doesn't make it clear what's missing from Python. If the immutable subclasses would be valuable, I'll gladly make a module for it. There's already frozenset. A frozendict didn't seem as useful (according to core devs).
- xapata 9y agoPlus, I forgot that sets have nice operators for union, intersection, etc. So you don't need to override anything there.
- inexplicably 9y agoSane name spaces, no hacks like "nonlocal", immutable bindings by default, more fluid syntax, functors, encapsulation, tail call elimination.
- xapata 9y agoYou're using terms like "sane" and "hacks" that have negative connotations without acknowledging the benefit those design decisions provided over the history of the language. Language design involves trade-offs. For example, tail call elimination would obfuscate tracebacks and make the interpreter more complex. Need is different than want. You might want tail call elimination, but you don't need it to write elegant functional style code.
- Beltiras 9y agoNot really no. You can use the idioms but FP python is not. Guido has declared that there will never be tail call optimization to name one glaring incompatibility.
- blain_the_train 9y agoIt also lacks multimethods, or pattern matching which mean you need classes for polymorphism. Which makes it not suitable for for imo