Object-Oriented Programming
Imagine we are making a program for a blog. We will be storing data related to posts that have been written for the blog.
An option for our application would be to just use a database, with no program written. Every time a post gets written, the author has to load up a database and put the title and content of the post in it.
This means that the author needs to have access to the database, and needs to know how to update the database with the new data.
We can’t really expect that, so we agree that we need a program written with some sort of user interface. In this user interface we could load the posts, which would bring up some information about them, including, for example, the title or content.
While we develop our program, remember that each one of these posts is just a row with some data in a database somewhere.
In order to make our program understand that the data it is dealing with has these properties, we create a Class which has two properties: title and content. It could look something like this:
class Post:
def __init__(self):
self.title = "This is a post title"
self.content = "This is some content"
Notice this isn't one specific post, but this is us just telling our program that this is the thing it is going to be working with. Now, lets create one post in our program:
import post
post_one = Post()
post_two = Post()
We are telling our program "hey, these things you understand called post_one and post_two are objects of type Post! You know they have these two properties: title, and content".
And then our program says "cool I got it."
In our class definition we stated that a post's title and content are "This is a post title" and "This is come content", respectively. Because it is defined at a class level, both post_one and post_two have the same title and content.
That’s what a Class does: define that the properties exist (and, optionally, give them values).
This is not particularly useful, because whenever we create a Post it's going to have the same title and content. We need a way to change the title and content depending on the post we are creating. We can do this by passing arguments to the __init__ method, like so:
class Post:
def __init__(self, title, content):
self.title = title
self.content = content
Now we’ve got this constructor, we can tell our program “we’re going to tell you that this post exists, and also give you its properties so you can use them”.
import post
post_one = Post(title="Object-Oriented Programming", content="The content goes here!")
post_two = Post(title="Second blog post", content="You can have different content in this Post.")
Now the program knows post_one.title is "Object-Oriented Programming". But we can create many objects, not just one! They all have the same properties, but can have different values for them.
import post
post_one = Post(title="Object-Oriented Programming", content="The content goes here!")
post_two = Post(title="Second blog post", content="You can have different content in this Post.")
post_three = Post(title="Third post", content="This is empty, for now!")
print(post_one.title)
print(post_two.content)
print(post_three.title)
There we’ve got three objects, they all have different values, but we only had to define what their properties are (note: not their values) once, when we defined the Class. We did this by initializing properties in the __init__ method, but setting their values via the arguments passed to it.
Object actions
Objects can not only store data. They can also store actions, or things the object can do. Usually, objects can do things on themselves.
For example, a Post object could clear its contents. To do this we would create a method inside the Post class like so:
class Post:
def __init__(self, title, content):
self.title = title
self.content = content
def clear_contents(self):
self.title = ""
self.content = ""
That method simply make the post's title and content equal to an empty string, which clears them, removing the values. This could be used in a program like so:
import post
post_one = Post(title="Object-Oriented Programming", content="The content goes here!")
print(post_one.title) # prints "Object-Oriented Programming"
post_one.clear_contents()
print(post_one.title) # prints ""
Our program now has the objects, and we’ve understood that objects can be used to model real-life objects, such as blog posts, but also could be used to model animals, computers, chairs, or anything else you can think of!