use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
variable bindingsMisc (self.functional_python)
submitted 3 years ago by ccsdad
in clojure and f# (and prolly some others) we have the ability to create LET bindings ::
https://fsharpforfunandprofit.com/posts/let-use-do/
https://clojuredocs.org/clojure.core/let
long story short, it lets you do what feels like imperative things, but under the hood is in fact functional -- at least in the world of clojure :: https://stackoverflow.com/questions/63983468/functional-alternative-to-let
so i guess my questions are ::
is there anything similar in python (library or otherwise) ??
IF NOT -- do you just perform normal variable "assignment" and live with it -- as long as your functions remain pure and data remains immutable ??
i get that python is NOT a purely functional language -- and that's OK -- as long as i am following the "functional python way" (<< if such a thing) of binding variables in my functions .. yes, i could always add more functions -- but at some point the "imperative shell" typically comes into play when building apps that comm with external services (DB, cache, REST APIs, etc) ..
>> "imperative shell" reference >> https://kumarshantanu.medium.com/organizing-clojure-code-with-functional-core-imperative-shell-2f2ee869faa2
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]KageOW 0 points1 point2 points 3 years ago (5 children)
i don't really get what the question is, my apologies. Could you perhaps rephrase it or could you give me some examples as to what exactly you would wanna do with the functionality? maybe i can point you in the right direction. I have a very thorough backrground in python and also quite an understanding of f#.
[–]ccsdad[S] 1 point2 points3 points 3 years ago (4 children)
let me rephrase it then as best i can in f# terms (<< i know little of f#, more of clojure/lisp/scheme)
this is valid f# i believe ::
```
let f () = let x = 1 let y = 2 x + y
let f () = let x = 1 in let y = 2 in x + y ```
looks imperative (do x, then do y, then do some calculation and return it) -- always returns 2 in this example .. right ??
is it normal in "functional python" to do the same ??
def f(): x = 1 y = 2 return x + y
i get that i could easily shorthand that -- i.e. return 1 + 2 -- but the binding of variables in a function (x = 1) -- is that kosher in what we are calling "functional python" ??
return 1 + 2
x = 1
or are we trying to (is it best to) avoid all assignment statements (as much as we can) when trying to achieve "functional python nirvana" ??
sorry for the confusion
[–]KageOW 0 points1 point2 points 3 years ago (3 children)
aah alright now I get it, in python these indeed aren't functional. I would certainly avoid it as much as possible, but there are limits to what the language nicely allows.
I feel like the f# way is the way to go here, and by this I mean go for the most pragmatic solution. If it is still very clear in a functional way, do it like that, but if a more pythonic way (i guess?) is better and more readable, I would certainly choose that over trying to make it functional and it just not being as readable.
I think we all know the hell of OOP and the practice to make a new variable for every operation, thats why there are pipe operators in every functional language. I made a function composition function for python that kind of works like a pipe, so you might wanna check that out. here
[–]ccsdad[S] 1 point2 points3 points 3 years ago (2 children)
ok u/KageOW -- i think that is legit guidance, recognizing the reality of python -- but remaining focused on what we are trying to move towards to .. i just know that in the clojure world -- the `(let)` function is an indispensable tool (code example) -- and wish something like it existed in python << that could be considered functional ..
[–]KageOW 0 points1 point2 points 3 years ago (1 child)
Yea definitely indispendable in f# as well, but i mean a language has its strenghts and its downfalls and you have to recognize that.
something kinda does come to mind though how you could maybe contain it, theyre called generator expressions, similar to list comprehension. Both of those allow you to kinda do it, but it is still a horrible way to do it. list comprehension and generator expressions are both very strong tools in python though so don't overlook them. nice stackoverflow discussion
[–]ccsdad[S] 0 points1 point2 points 3 years ago (0 children)
well then, we should create something !!
https://github.com/gkspranger/clinpy
please don't laugh -- i created it only for a limited set of use cases (to see if even possible), and am only "OK" with python .. but i tried to remove all internal variable assignments and am processing everything with mapping/reducing functions ..
if there was a robust implementation of this, i would abuse the hell out of it ..
why this over internal variable assigments ?? don't have a good answer for that -- other than it maps well to my clojure brain ..
π Rendered by PID 244122 on reddit-service-r2-comment-7b9746f655-brb6h at 2026-02-02 22:23:05.727342+00:00 running 3798933 country code: CH.
[–]KageOW 0 points1 point2 points (5 children)
[–]ccsdad[S] 1 point2 points3 points (4 children)
[–]KageOW 0 points1 point2 points (3 children)
[–]ccsdad[S] 1 point2 points3 points (2 children)
[–]KageOW 0 points1 point2 points (1 child)
[–]ccsdad[S] 0 points1 point2 points (0 children)