Style Q: Instance variables defined outside of __init__
On Tue, 20 Mar 2018 11:14:13 +0000, Alister via Python-list wrote:
> but why would a functional programmer be programming an OOP class?
I read a really good blog post a while back, which I sadly can no longer
find (I had it bookmarked until Firefox ate my bookmarks) that discussed
exactly that question.
What they came up with is that its really hard to do useful work for
large applications with 100% pure functional programming styles. As you
add functional techniques to your code, you find your code quality
increasing. Things like pure functions with no hidden state and no side
effects can have a massive positive effect on code quality -- up to a
As you approach closer and closer to 100% pure functional code, you get
fewer improvements and your code gets harder to write and maintain.
(Think about things like dealing with user preferences, and having to
pass them around from function to function. Think about functions that
don't need a parameter themselves, but need to call a function that does.
Think about data processing without any mutable data structures.
Sometimes a modicum of state is exactly what we need.)
He maintains that the sweet spot is about 80% functional, and 20% OOP.
That seems to be about right to me: write classes with just the bare
minimum state you need, and have your methods work using a nearly-pure
functional style: they should take arguments, they should return results,
they should rarely operate by side-effects, etc.
Most of Python's design is like that. Apart from a few mutable data
structures, Python's built-ins tend to be immutable. We don't do things
finder = SubstringFinder(start=1, end=55)
position = finder.find()
rather we use the semi-functional style:
position = string_to_search.find(substring, start=1, end=55)
Don't be fooled by the dot method call syntax: that's exactly equivalent
to the purely functional style
find(string_to_search, substring, start=1, end=55)