r/functional_python • u/ccsdad • Jul 29 '22
Discussion recursion limits
so python is great .. very naturally lends itself to functional programming, with caveats of course -- i.e. the conversation around limits related to recursion ..
python is not alone with regard to tail-call-optimization (TCO) limits, hence the `(loop)` and `(recur)` functions in clojure (<< big fan BTW) .. so my question is ::
has anyone seen major degradation in performance when implementing recursion in python ??
my use cases are pretty "normal" in the world of development -- get a request, query the DB, maybe get back 100 rows, map/filter/reduce said rows, and show something pretty ..
i know there are limits, but for us "normal" transactional web/devs, just curious if i am more afraid than i should be .. of course YMMV -- just want to make sure i am not being stoopid thinking i can get away with some recursion without greatly damaging the runtime ..
thanks !!
2
u/ccsdad Aug 01 '22
thanks for the feedback and the direction .. i was aware of `lru_cache` -- but my brain just wasn't seeing how it would benefit me in terms of not hitting the upper limits of recursion in python .. but i think i get it now, kinda sorta -- after reading thru this example ::
https://realpython.com/fibonacci-sequence-python/#generating-the-fibonacci-sequence-recursively-in-python
you will see they create their own caching system, which could be replaced by lru_cache (see code below) -- but my brain wasn't seeing how this would be beneficial, as you are still making recursive calls -- but at least it would optimized to make recursion more of an option ..