6 ms·
When I write Python it’s exclusively without OOP. It’s totally possible to write Python in a solely functional manner. You make heavy use of NamedTuple, Enum, i
by ilovecaching 8y ago
When I write Python it’s exclusively without OOP. It’s totally possible to write Python in a solely functional manner. You make heavy use of NamedTuple, Enum, itertools, functools, and mypy annotations.
I can approach writing Python essentially the same way I’d write Haskell using the above strategy. I think it looks really nice.
I’ve turned a few people away from the dark path of OOP by writing in this style as well. They’re usually turned when they see how easy it is to test just regular functions with no need for magic mocks and the like.
- shackenberg 8y agoCan you point to any good tutorials for replacing OOP with functional patterns in Python? I often use OOP to semantically group functionality and "manage" state e.g. avoiding methods with multiple variables or cases where I have to pass one (not changing) variable to multiple functions.
- virmundi 8y agoSounds like you discovered that OOP and Functional are the same thing. Closures are the poor-person's objects. Objects are the poor person's Closures. If you never mutate the internals of an Object, or mutate with copy, isn't just a coherent closure? Checkout http://wiki.c2.com/?ClosuresAndObjectsAreEquivalent http://wiki.c2.com/?ClosuresAndObjectsAreEquivalent
- shackenberg 8y agoIf they are the same thing, why does the parent commenter prefer Functional?
- virmundi 8y agoPersonal choice. Some people like the idea of splitting the data from the functions that operate on it. Others don't like mutability and presume that OOP requires it. Scala and other languages make it easy to have immutability and OOP. Look at Clojure. The interfaces look a lot like Object definitions.
- mlcdf 8y agoWould you mind sharing some code (or tutorials)? I'm intrigued!
- goatlover 8y agoWhy not just use Haskell? Python has OOP baked into the language. Also, your responding to an article on introducing OOP with how not to use OOP, which seems tangental and deserving of it's own thread.
- mavelikara 8y ago+1. When GP leaves a job and a "Python Programmer" is hired to replace him/her, their life will be hell. At that point one can only hope that GPs next gig is at a Haskell shop where every single function returns an IO action :)
- goatlover 8y agoUsing a programming language idiomatically is better than using it to match one's preferred paradigm. If the paradigm is more important, then use the right tool for the job. My understanding is that Guido and the community never intended Python to be a functional PL.
- cgopalan 8y agoIMHO, Python has enough functional goodies to accomplish a lot with just functions and provided datastructures. I only use OOP as a technique to address specific issues, rather than treat it as something fundamental as writing functions.
- ilovecaching 8y agoIn my experience what matters is the quality of the code regardless of the paradigm used. A well written piece of functional Python with comments and documentation will be much easier to understand than poorly written OOP. FP is also quite idiomatic. Pythons list comprehensions are quite powerful, and the libraries I mentioned in my previous comment are all part of the standard library. There are third party libraries that attempt to make Python even more functional.
- olavk 8y ago
- vram22 8y agoHow about {list|dict|set} comprehensions? I don't know if they are considered part of functional programming (and the definition of FP seems to be somewhat loose anyway, AFAICT - correct me if I'm wrong, anyone), but they can be nice to use. They can even be nested (never gone beyond 2 levels myself) and if/else clauses can be in them too. E.g.: >>> def odd(n): return n % 2 == 1 ... >>> [ i if odd(i) else -i for i in range(1, 11) ] [1, -2, 3, -4, 5, -6, 7, -8, 9, -10]
- cgopalan 8y agoIndeed comprehensions are part of many functional languages like Haskell, Ocaml, Clojure.
- vram22 8y agoGood to know, thanks. In fact, now I think I remember reading that Python got the idea of comprehensions from Haskell.
- ilovecaching 8y agoComprehensions are a key part of functional programming in Python. Python lambdas are fairly awkward to use. Comprehensions provide a better syntax for transforming data in many cases.
- vram22 8y agoYes, lambas don't allow statements in them, only an expression. In [8]: f = lambda x: x * 2 In [9]: f(2) Out[9]: 4 In [10]: g = lambda x: if x % 2 == 0: x * 2 else x * 3 File "<ipython-input-10-f7248669b8fd>", line 1 g = lambda x: if x % 2 == 0: x * 2 else x * 3 ^ SyntaxError: invalid syntax
- mvanbaak 8y agoIn python, everything is an object ;)
- leshow 8y agoYou can't write python 'without OOP'. Almost everything you do requires instantiating some class, or called a method on a class. Unless you write everything globally with no mutation. You can write python in a functional style, but you're still going to use OOP constructs.
- virmundi 8y agoAside from instantiating a DataFrame, I've written scikit/numpy/pandas code with nothing more than functions and data structures.
- pjmlp 8y agoFunctions and data structures are objects in Python.
- ilovecaching 8y agoWe’re talking about style here, not the technicality of whether something is implemented as an object or not.
- pjmlp 8y agoI wonder how you manage to do such a feat, given that all Python data types are objects. > from inspect import type > type([]) => <class 'list'> > type(lambda x : x) => <class 'function'> > type(1) => <class 'int'> > type([x * x for x in [1,2,3]]) => <class 'list'> > type((x * x for x in [1,2,3])) => <class 'generator'> > ...
- ilovecaching 8y agoWe’re talking about style here, not the technical details of the implementation.
- deleted 8y ago[deleted]