OOP in Python
OlauPla

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:

Ouput:

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:

Output:

Finish

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
OldWizard209

Hey. Great tutorial but a few notes about a couple of things you wrote.

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 is called a dunder method, recognized by the pair of two underscores around the name.

The init method takes 3 parameters self, name, and age.

This sentence makes it seem like the __init__ method takes three parameters by default. But that's not true. The first parameter has to be the instance of the class. After that, there can be as many parameters as you want.

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.

This is true, but the instance of the class doesn't always have to be self. It can be named anything, like potato. But the programming convention is to use self. I just made this note to avoid letting people think that self is the only thing that can be put as a first parameter. Here is an example:

But it is also important to note that self is the best keyword to use.

The self.name = name and self.age = age takes the values associated with each parameter and assigns it to name and then age.

Once again the keywords after self that assign the attributes can be named as any ordinary variable. They don't have to be the same as the parameter names.

These are just a few things I had to point out. But other than that, great tutorial.

OlauPla
OlauPla

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