OOP in Python
OlauPla (169)

Object-oriented programming

Today I am going to talk about Object-oriented programming I will use OOP to reference it through out the tutorial.

This tutorial is not directly meant for python beginners. What I mean with this is: If you just started learning python as your first programming language this tutorial is not for you. Learn the basics first.

If you think something is not well explained or simply incorrect please leave it in the comments asp! I will fix it.

What do I do if I am stuck or don't understand something?

  • Think for a bit
  • Google it or ask it in the ask question of repl.it
  • If you still don't get it feel free to leave a comment

Let's get started!

What is OOP?

OOP is one of the most effective approaches to writing
software. In OOP you write classes that represent real-
world things and situations and you create instances of
those classes. For example take for a class "Human",
everyone is an instance of that class.

A class example

As I mentioned before the example of the "Human" class
we are going to code it.

This will output the following:

Wow that was a big step! Let's explain everything:

First we define a class named "Human".
Then we define a special method for the class. The init method, this method will run when an instance of the "Human" class is created.
The init method takes 3 parameters self, name and age.
The self parameter is required in all method definitions and it must go first, before the other parameters. The self parameter is passed automatically to every class method. The self is a reference to the instance itself. Then we define name and age variables attached to the self. The self.name = name and self.age = age takes the values associated with each parameter and assigns it to name and then age. This might be hard to understand, and explain. Doing that makes the variables name and age available to the whole class through the self. Variables like that are called attributes.

Later we define class methods that define the things that a Human can do.

Finally we create an instance of the class human, pass in the arguments name and age. Then we print their values.
We also call the class method eat, then the birthday. We observe that the attribute age has been changed. And to end we call the sleep method.

Creating multiple instances of a class

Creating multiple instance of a class is possible, let's use the previous class as an example:


As you can observe, multiple instances can be created.

Modifying attribute values

You can modify the age or the name of Human whenever you like. It is more easy to create methods to do that but you don't need it. Simply do this:



I will do another tutorial on OOP if you guys like this one.

If I make another I will talk about class inheritance and default attributes

Thank you for reading my tutorial, I would like to give credit to: Eric Matthhes, his book Python Crash course helped me make this tutorial!

You are viewing a single comment. View All
OlauPla (169)

Also I knew about the self thing but i didn't want to say it, i thought it could confuse but your right