Live data from Hacker News

Functional Python Programming

docs.python.org

81–90 of 105 posts

Re: Functional Python Programming

#81

What's so useful about iterators and generators. The article says how to use them but not why. If you already know how to make list comprehensions and use "for elt in coll", do they let you do anything new?

It's the difference between evaluating all the elements of a list, consing them, and returning it all at once and evaluating the elements one at a time (i.e. lazy evaluation). Saves memory and time and it gives you a chance to quit early.

Re: Functional Python Programming

#82

Earlier quoted context omitted.

I love both python and functional programming and I do write functional style python routinely. One barrier I hit is there is no immutable dict type. There is MappingProxyType but it's insufficient. I prefer using custom dataclass like objects built with pydantic and type checked with mypy. It's better than dict and can be made immutable but requires tons of boilerplate code which is a bit unpythonic.

> there is no immutable dict type Does the namedtuple not suffice? Apologies if I'm being dense. - - - - It's not an immutable dict type (for one thing, this has linear lookup, the BTree would be better) but it's fun: https://stackoverflow.com/questions/13708701/how-to-implemen... In Python: from functools import partial def empty_dict(key): raise KeyError def _dict_add(dictionary, key, value, lookup): return value i…

> My attitude is that if you really need them you should switch to e.g. OCaml or something that does them right.

I strongly disagree. I personally think there is no programming problem where types aren't useful. When I write code, I think in terms of types, and rarely in terms of anything else. Python is useful in tons of problems OCaml isn't such as data science, statistics etc. Besides python has a very large ecosystem which matters in order not to reinvent the wheel.

Re: Functional Python Programming

#83
post #63
post #54

Earlier quoted context omitted.

Readability isn't the best. Also what you present here is method chaining and not functional pipes.

Is the difference mostly syntactic?

with dot chaining you are restricted to only the functions that the API you're working with provides. With functional pipes you can use whatever functions you want.

Re: Functional Python Programming

#84
post #21

shameless plug: I maintain a small library to do functional pipes. You can write: ( range(10) | Map(lambda x: x * 10) | Filter(lambda x: x % 2 == 0) | Reduce(lambda a, b: a + b) ) instead of: x = range(10) x = map(lambda x: x * 10, x) x = filter(lambda x: x % 2 == 0, x) x = reduce(lambda a, b: a + b, x) and more. https://tandav.github.io/pipe21/

if folks like this and use JS, there is a very similar library for that ecosystem: https://ramdajs.com/

Re: Functional Python Programming

#85
post #8

Sadly, Python is a pretty poor functional language. The core of functional programming is about avoiding mutable states , not much about anonymous functions or passing functions as data. To do proper functional programming in Python, there should be IMO: - a way to enforce non-mutable variables/objects; - non-mutable collections; - proper support for recursion and tail-recursion optimization; - a better syntax for an…

Not having immutability isn't the worst thing IMO. I guess if you can't trust your coworkers to not modify their inputs, it's a different story.

Re: Functional Python Programming

#86
post #8

Sadly, Python is a pretty poor functional language. The core of functional programming is about avoiding mutable states , not much about anonymous functions or passing functions as data. To do proper functional programming in Python, there should be IMO: - a way to enforce non-mutable variables/objects; - non-mutable collections; - proper support for recursion and tail-recursion optimization; - a better syntax for an…

> The core of functional programming is about avoiding mutable states,

I can't keep up, does this mean scheme is not a functional language anymore by modern definition?

Re: Functional Python Programming

#87

Thanks for posting this. Many years ago (2006?) I was a just-out-of-university programmer and a friend of a friend who wrote Python for Canonical sat me down and tried to teach me functional programming using Python. I was a poor learner: I didn't 'get it' (functional programming in general) and, to my regret, learned nothing. Much time has passed and I hope I'm a more aware and open-to-new-things person today :) I u…

IMHO broaden your boundaries a bit and learn a lisp or clojure-based functional style, like grab a getting started in clojure book and work through it. You will learn a lot more than this guide goes into, it's really just looking at functions in a functional style but doesn't spend nearly enough on data structures and state which are really the core of functional programming (and what python doesn't do well out of th…

I’ll second this. Learning Clojure radically changed how I think about programming, regardless of what language I program in now. Both because of the restrictions of FP itself, but also (and, IMO, at least as importantly) because pervasive FP solutions at the language/stdlib level are an excellent source of inspiration and guidance for “thinking in” FP.

The great thing is that FP as an approach does translate very well to most imperative/OO contexts. It just isn’t necessarily very obvious how it will, until/unless you’ve been fully immersed and embraced it.

Re: Functional Python Programming

#88
post #8

Sadly, Python is a pretty poor functional language. The core of functional programming is about avoiding mutable states , not much about anonymous functions or passing functions as data. To do proper functional programming in Python, there should be IMO: - a way to enforce non-mutable variables/objects; - non-mutable collections; - proper support for recursion and tail-recursion optimization; - a better syntax for an…

> The core of functional programming is about avoiding mutable states, not much about anonymous functions or passing functions as data.

I disagree since any paradigm can be done either with or without mutable state. So it is not something that defines functional programming.

In any case you can also use immutable data types with python anyway.

The main annoyance with python is in my opinion your last point: The lambda syntax is too limited. So it is necessary to define lots of nested small functions instead.

Also python syntax is not as nice in chaining function calls over multiple lines.

Re: Functional Python Programming

#89

Thanks for posting this. Many years ago (2006?) I was a just-out-of-university programmer and a friend of a friend who wrote Python for Canonical sat me down and tried to teach me functional programming using Python. I was a poor learner: I didn't 'get it' (functional programming in general) and, to my regret, learned nothing. Much time has passed and I hope I'm a more aware and open-to-new-things person today :) I u…

I find that learning functional style in a language that does not enforce it, is extra difficult. If the language does not provide guard rails, then new learners will fall back on non-functional methods, and not even realize it.

I very much support this view. If you want to teach someone functional programming, you go straight to Haskell or a Lisp. They don't need to master the language or anything, but the cold shower of "There are fundamentally different paradigms in programming than those I am used to" combined with a language that will allow nothing but functional code tends to work a lot better than "Hey guys! Let's torture Java to support map/filter/reduce!"

Re: Functional Python Programming

#90
post #62

Fun to work on doing FP in languages that don't really support it, but in my view a language has to be built for FP for it to be a practical option in any real applications. Several obvious reasons for Python being a poor lang in which to do FP: - mutable data structures - no built-in function composition - limited support for HOF - no tail call optimization (AFAIK) - performance in general isn't great and I imagine…

It doesn’t have to be all or nothing though. I’ve definitely seen (and written) functional Python in ‘real applications’ sprinkled here and there in an otherwise normal codebase. In fact, I’d wager that any Python programmer worth their salt would suggest using a list comprehension and a lambda over a loop in most data transformation situations. Also currying comes in handy often once you have it in your toolkit. It’…

No, it doesn't need to be all or nothing, but the scenario I was directing my point at was where people try to "do FP in lang X", not just sprinkling in functional concepts here are there. At this point, almost every language has some functional programming concepts in it, but I wasn't addressing that. I could've been clearer, but I've seen many cases where people would rather be programming in Haskell or some other functional language and then when "forced" to use some other language, like Python, they try to essentially recreate Haskell in that language and my point is that this really doesn't work or, at the very least, it's more trouble than it's worth and the resulting codebase is a complete mess because you've tried your hardest to do FP in a language that ultimately isn't suited for it. You are absolutely right though that it needs not be all or nothing. To the extent you can rely on some of these constructs and enable equational reasoning by avoiding mutation, etc. you'll be better off, no doubt (I do this too in Python and other languages because wherever I can make it easier to reason about my code, I will do so). I'm just cautioning against trying to do something akin to pure FP in Python.
Post reply on HN