[Tutor] Functional Programming in Python
Alan Gauld
alan.gauld at btinternet.com
Thu Apr 2 19:07:18 CEST 2015
On 02/04/15 17:18, WolfRage wrote:
> to programming but acknowledge that it is far from Functional,
> especially since this is mostly impossible in Python.
I'm not sure what you mean is impossible? The specific issues
you are having or the general FP paradigm?
Python has fairly good support for FP, much better than
most non-FP-specific languages.
> Questions:
> What are the best practices to create more Functional Python?
Everything goes in functions which do not change their inputs and return
outputs.
Use tools like itertools and functools to avoid explicit loops.
> What are your thoughts on Functional in Python?
It's good enough to use where the problem suits FP. It's no
more a pure FP language than it is a pure OOP language.
But in both cases you can get close enough for practical
purposes. What is your problem scenario that makes FP seem
like the best idiom?
> Currently I am re-writing functions to reduce their side effects.
Good, nearly always the best way.
> I am also removing the state from objects and putting it into a closure
> type function.
That may or may not be a good idea. FP object handling can
get awfully messy and awfully inefficient.
> However with callback based systems (GUI) this seemed impossible, so
> instead I am abusing a coroutine to maintain the state of the application.
Trying to force a non-FP framework(eg a GUI)_ to work in an FP
way is usually an exercise in frustration.
> But is it abuse or does it seem like a good way to handle the callback
> system? The benefit to me at this time is limited, but any errors in
> state are confined to a single location, which is nice.
That should be true in an OOP based system too. The whole point
of OOP is to contain and hide state (as opposed to eliminating it)
> What do you think about using a coroutine to handle state, how would you
> do it better in a callback based system.
Without seeing how you are doing it we can't comment on *better*.
We need a baseline. Your approach may be brilliant, or it may
be awful, we can't tell.
But remember that there is no such thing as the perfect paradigm.
FP has many strengths but it has many weaknesses too. As does OOP.
Even procedural programming has advantages over OOP and FP in
some circumstances. By all means learn the paradigms, but don't get hung
up on any one. They all play a part.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list