5
I have been dabbling in Python for some time now and have written some really easy apps. But I alway...
since 2 years, 10 months ago
1 of 1
Tip Reveddit Real-Time can notify you when your content is removed.
your account history
Tip Check if your account has any removed comments.
view my removed comments you are viewing a single comment's thread.
view all comments


OOP mainly focuses on classes.
Classes can hold two things: data, methods.
A class without methods would be like a struct in where you just hold some data. Like a point class that holds x and y but cannot do anything with it.
A class without data would be like a library. Here the different methods would be a collection of similarly themed functions. For example Math could have square roots, logarithm, even derivation, but doesn't need to hold any data.
A class without data or methods... it might exists, it might also have some utility, but it escapes me.
A class with data and methods. You hold some information, an object can do actions that modify its information.
For example, let's take the falling sand problem and do it several ways, focusing on the sand we always have a set that holds all the blocked positions.
minimal abstractions
Iteration1: Have a initial set of coordinates.
Iteration2: Can we go down? go down. Iteration2.
Otherwise, can we go down left? go down left. Iteration2.
Otherwise, can we go down right? go down right. Iteration2
Otherwise add to blocked. Iteration1.
Are we out of bounds? STOP
Library functions
bool Sand.CanMoveDown(pos) -> can it go down?
bool Sand.CanMoveDownLeft(pos) -> can it go down left?
bool Sand.CanMoveDownRight(pos) -> can it go down right?
pos Sand.MoveDown(pos) -> move down
pos Sand.MoveDownLeft(pos) -> move downLeft
pos Sand.MoveDownRight(pos) -> move downRight
bool Sand.CanMove(pos)-> CanMoveDown(pos) or CanMoveDownLeft(pos) or CanMoveDownRight (pos)
pos Sand.Move(pos) -> CanMoveDown(pos)? return MoveDown(pos) else CanMoveDownLeft(pos)? return MoveDownLeft(pos) else CanMoveDownRight(pos)? return MoveDownRight(pos)
Sand.Block(pos) -> adds to set of blocked.
pos Sand.Init -> gives the initial coordinates.
bool Sand.OutOfBounds(pos) -> checks for final state.
pos = Sand.Init
while(not Sand.OutOfBounds(pos)) do
Sand.CanMove(pos)? pos=Sand.Move(pos) else Sand.Block(pos), pos=Sand.Init
full oop with mutable state
Sand contains pos
bool Sand.CanMoveDown -> can it go down?
bool Sand.CanMoveDownLeft -> can it go down left?
bool Sand.CanMoveDownRight -> can it go down right?
Sand.MoveDown -> move down
Sand.MoveDownLeft -> move downLeft
Sand.MoveDownRight -> move downRight
Sand.Move -> CanMoveDown? MoveDown else CanMoveDownLeft? MoveDownLeft else CanMoveDownRight? MoveDownRight else Block, Init
Sand.Block -> adds to set of blocked.
Sand.Init -> sets the initial coordinates.
bool Sand.OutOfBounds -> checks for final state.
Sand.Init
while(not Sand.OutOfBounds) do
Sand.Move
Alternatively, with some changes, we can do as before:
Sand.Init
while(not Sand.OutOfBounds) do
Sand.CanMove? Sand.Move else Sand.Block,Init
End of examples
As you see OOP hides some things inside classes, so the main code is simpler to understand. Yes, the total number of lines is more, but it can be read more easily, and the separation of concerns makes it easy to know where to do modifications.