News for hackers. Stuff that matters.
Re:Introduction to Object Oriented Programming in Python by ilovecaching on Wednesday October 24, 2018 @11:15AM (#18293135)
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.

Re:Introduction to Object Oriented Programming in Python by goatlover on Wednesday October 24, 2018 @11:51AM (#18293466)
Why 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.
Re:Introduction to Object Oriented Programming in Python by mavelikara on Wednesday October 24, 2018 @12:01PM (#18293583)
+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 :)
Re:Introduction to Object Oriented Programming in Python by ilovecaching on Wednesday October 24, 2018 @12:50PM (#18294119)
In 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.

Re:Introduction to Object Oriented Programming in Python by olavk on Wednesday October 24, 2018 @01:51PM (#18294714)
Well written code is obviously better than badly written code. But why assume the functional-style is better written than the idiomatic Python? I think a good developer understand the context they work in rather than try to fit a square peg in a round hole.

I wouldn't like to maintain Haskell code written by someone who thinks everything should look like Python. And vice-versa.

Re:Introduction to Object Oriented Programming in Python by tarboreus on Thursday October 25, 2018 @11:56AM (#18301888)
Python code doesn't have to be OOP to be idiomatic. That said, functional code that uses lambdas, map, etc. is generally not idiomatic Python. I've seen a lot get done just with functions, list comprehensions, and Python-specific magic without using OOP, though. Guess that's mostly just imperative on steroids, though.
Re:Introduction to Object Oriented Programming in Python by ilovecaching on Wednesday October 24, 2018 @02:19PM (#18294961)
> square peg in a round hole

Python is a multi-paradigm language. I have not found Python to be better at OOP versus imperative or functional styles of programming.

For me, functional programming provides a closer to model to how I think about problems. This is true for many people. One should choose the style that provides the easiest translation of thought to code.

Re:Introduction to Object Oriented Programming in Python by olavk on Wednesday October 24, 2018 @02:48PM (#18295238)
If you only write code which you yourself will read and maintain, then you should definitely do whatever you feel works best for you and matches your way of thinking.
Re:Introduction to Object Oriented Programming in Python by wrmsr on Wednesday October 24, 2018 @04:13PM (#18295927)
Python's lambda is very intentionally limited precisely to steer people away from overly functional code. Don't write Haskell in Python, and equally don't write Java in Python.
Re:Introduction to Object Oriented Programming in Python by rptr_87 on Thursday October 25, 2018 @04:52AM (#18299460)
I’m currently working on a large C++ project... which has layers and layers of indirections...

It’s so hard to even check where the actual implementation is....

There are no comments in code, no documentation whatsoever...

Re:Introduction to Object Oriented Programming in Python by goatlover on Wednesday October 24, 2018 @12:10PM (#18293687)
Using 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.
Re:Introduction to Object Oriented Programming in Python by cgopalan on Wednesday October 24, 2018 @12:16PM (#18293761)
IMHO, 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.
Re:Introduction to Object Oriented Programming in Python by yen223 on Wednesday October 24, 2018 @05:02PM (#18296356)
As a Python developer, I'd much rather maintain a system written in a functional style, over one written in a Java-like OO style.

Refential transparency and simple data structures makes code much simpler to understand, and leads to fewer surprises when the code is being modified.

Re:Introduction to Object Oriented Programming in Python by ilovecaching on Wednesday October 24, 2018 @12:46PM (#18294072)
Python is one of the most popular languages in the world, it’s quite common to encounter it when you join a new buisness. It’s also the language many people learn to program with (I did).

Anyone who learns Python will necessarily learn OOP as it’s the de facto style. The post is a resource for that. My comment is meant to encourage people to consider the alternative ways you can approach problems in Python.

Re:Introduction to Object Oriented Programming in Python by shackenberg on Wednesday October 24, 2018 @11:33AM (#18293275)
Can 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.
Re:Introduction to Object Oriented Programming in Python by virmundi on Wednesday October 24, 2018 @12:02PM (#18293599)
Sounds 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

Re:Introduction to Object Oriented Programming in Python by shackenberg on Wednesday October 24, 2018 @12:13PM (#18293722)
If they are the same thing, why does the parent commenter prefer Functional?
Re:Introduction to Object Oriented Programming in Python by virmundi on Wednesday October 24, 2018 @12:42PM (#18294031)
Personal 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.
Re:Introduction to Object Oriented Programming in Python by pjmlp on Wednesday October 24, 2018 @05:14PM (#18296461)
I 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'>
    > ...
Re:Introduction to Object Oriented Programming in Python by ilovecaching on Wednesday October 24, 2018 @07:09PM (#18297265)
We’re talking about style here, not the technical details of the implementation.
Re:Introduction to Object Oriented Programming in Python by vram22 on Wednesday October 24, 2018 @11:56AM (#18293528)
How 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]

Re:Introduction to Object Oriented Programming in Python by cgopalan on Wednesday October 24, 2018 @12:02PM (#18293611)
Indeed comprehensions are part of many functional languages like Haskell, Ocaml, Clojure.
Re:Introduction to Object Oriented Programming in Python by vram22 on Wednesday October 24, 2018 @12:15PM (#18293750)
Good to know, thanks. In fact, now I think I remember reading that Python got the idea of comprehensions from Haskell.
Re:Introduction to Object Oriented Programming in Python by ilovecaching on Wednesday October 24, 2018 @03:52PM (#18295776)
Comprehensions 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.
Re:Introduction to Object Oriented Programming in Python by vram22 on Friday October 26, 2018 @07:51AM (#18308388)
Yes, 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
Re:Introduction to Object Oriented Programming in Python by leshow on Wednesday October 24, 2018 @02:03PM (#18294837)
You 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.

Re:Introduction to Object Oriented Programming in Python by ilovecaching on Wednesday October 24, 2018 @07:08PM (#18297255)
We’re talking about style here, not the technicality of whether something is implemented as an object or not.
Re:Introduction to Object Oriented Programming in Python by virmundi on Wednesday October 24, 2018 @04:00PM (#18295833)
Aside from instantiating a DataFrame, I've written scikit/numpy/pandas code with nothing more than functions and data structures.
Re:Introduction to Object Oriented Programming in Python by pjmlp on Wednesday October 24, 2018 @05:16PM (#18296479)
Functions and data structures are objects in Python.
Re:Introduction to Object Oriented Programming in Python by mlcdf on Wednesday October 24, 2018 @11:39AM (#18293339)
Would you mind sharing some code (or tutorials)? I'm intrigued!
Re:Introduction to Object Oriented Programming in Python by mvanbaak on Wednesday October 24, 2018 @12:26PM (#18293858)
In python, everything is an object ;)