
Introduction to Object Oriented Programming in Python - ScottWRobinson
https://stackabuse.com/introduction-to-object-oriented-programming-in-python/
======
denisw
> Object-Oriented Programming (OOP) is a programming paradigm where different
> components of a computer program are modeled after real-world objects.

This is a common, but dangerous way to introduce the object-oriented paradigm.
While it is true that many object-oriented systems end up having a few classes
that represent entities of its problem domain (such as, perhaps, bus stations
in a public transport app), most of them do not. These other classes are
abstract entities like data repositories, factories, controllers etc. that are
meant to split software into understandable pieces with clear responsibilities
that work together to make things happen.

The point is that this collaboration happens solely by these pieces sending
each other messages (calling methods) that describe their intent, without
needing to know how exactly the message is being processed. This style of
communication is the key characteristic of object orientation, and even an
introduction should probably put their focus on this aspect rather than
„modeling the real world“, which isn‘t really something that is done this way
in practice.

All this being said, I do see the appeal of the real-world rhetoric in that it
seems much more approachable than ralking about little communicating abstract
things. I wonder how to balance this.

~~~
olavk
I believe it _started_ as a way to model real-world object. Simula was
designed for simulations. But OO turned out to be useful for much more. The
simulation use case mostly live on in tutorial and examples, spreading
confusion to beginners.

But I also think the "message passing" metaphor has outlived its usefulness
the same way. A caller does not need to know how the callee is implemented.
Sure, but this is the same in any language with functions/procedure, this is
not something specific for OO. The idea of message passing is a bit deeper,
for example that the client does not even know it is calling a method. It
sends a message, and the object answers, even if it is just by throwing an
error that the message is not supported. This is how Smalltalk and (I think)
Ruby works, but not at all how a statically types language like C# works. And
it is not how Python works either. In Python you obtain a method reference
from an object and then invoke it. So if you apply the message passing
metaphor, your are actually passing the message to the _method_ rather than
the object itself. Which I think is just a confusing way to describe a method
call.

------
ilovecaching
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.

~~~
goatlover
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.

~~~
mavelikara
+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
:)

~~~
ilovecaching
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.

~~~
olavk
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.

~~~
ilovecaching
> 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.

~~~
olavk
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.

------
objektif
Can you guys please share tutorials you like for getting started with
functional programming in Python?

