Python Tutorial

Awesome Tutorial on Object-Oriented Programming in Python.

Object-oriented programming in Python.

One of the ways to simplify a program is to use objects, classes and functions.

In this tutorial, you will learn the very basics of Object-Oriented programming in Python.

But first, to the basics.

Basically, there are two methods in programming which are:

  1. Functional programming
  2. Object-oriented programming(OOP)

I’m sure that you have come across these terms.

If you were confused I will help you out.

Functional programming is about using functions and object-oriented programming is about using classes.

Object-oriented programming involves the use of objects, classes and methods to solve problems.

What’s the difference between a class and a function?

A function contains a block of code that performs only one operation. The operation of the function can be accessed by calling the function’s name.

While a class, contains several functions. The functions in a class can be accessed with an object.

The next difference is dumb but I said let me include it.

When you want to write a function you begin with the keyword “def”. For a class, you begin with the keyword “class”.

Traditionally, class names are capitalized while methods (functions in a class) are not capitalized.

Is Python Object Oriented?

Yes, Python is an Object-oriented programming language. Any one writing a program in Python can define new classes of object and use them. The programmer is not restricted to Python built-in classes of object. Examples of Python built-in classes are int, file, str etc.

That’s one of the reasons I love Python.

What is Class in Python Programming?

Before I define a class, let me tell you something (just an important side talk).

I love playing monopoly. The game of monopoly involves players rolling two dice and moving their piece on the board.

To explain what class in Python programming is, I will write a little program to simulate the game of monopoly.

The below code snippet is saved as monopoly.py in the source code download folder.

The monopoly.py program simply rolls the dice for player1 and player2 and returns the number of steps each player has to move on the board.

Note the following in this program

  1. The class name is Monopoly. It is good practice to start your class name with a capital letter.
  2. The objects of the class Monopoly are player1 and player2
  3. The function name rollDice is a method
  4. The attributes of player1 and player2 are die1, die2 and steps
  5. The instance variable in the class is “self”
  6. player1 and player2 are not only objects but are global variables because they can be used by the functions playGame and attribute

You will understand this terms as you continue to read.

So what is a class in Python programming?

Tony Gaddis defined a class as a code that specifies the data attributes and methods for a particular type of object.

Source: Starting Out with Python (second edition).

A data attribute is an element of a class. Therefore, die1, die2 and steps are attributes of the class Monopoly. You can also say that the attribute(s) defines the class.

The function attribute returns the attributes and their value in the class Monopoly. To get the attributes in the class, use this format,

object.__dict__

An object is a variable use to access the class. player1 and player2 are objects use to access the class Monopoly.

How To Store Your Classes in Modules

Storing your classes in a separate file hides the details of your code. It also allows you to use the class in another program by just importing it.

Note:

When you want to import a class to another program, make sure your program files are saved in the same folder.

To illustrate this, I will write a phone book program using class.

Open a new file editor and save this code snippet as phonebook.py

If you didn’t understand the basics of the above code, I advice you go through Python dictionary again.

Alright, now I will write a program that imports the Phonebook module and creates an instance of the Phonebook class.

Remember, an instance is an object associated to a class. Also, the __init__ function(method) will be explained later.

Open a new file editor and paste the below code snippet. Save the program as phonebook_demo.py.

Note:

You have to create an instance of the class Phone_book and that’s what this line of code (godson = Phone_book() ) does.

Am sure you now know how to save and import your class as modules.

How to make an attribute in a class private

To understand how to make a Python class attribute private, let us write a simple program to toss a coin.

Type the below code snippet in a new file editor and save it as TossDie.py

Here’s the output of the above snippet:

Observe that the value 10 in the output doesn’t change.

This is strange because first we imported the module random.

Secondly, random.choice(range(1,7)) returns randomly, an integer from 1 through 6.

Moreover, a die doesn’t have the value 10 on its side.

Therefore, why is 10 our output?

The problem is found in the function main.

By writing this line of code, myDie.side_up=10 , we change the value of the attribute, side_up(self.side_up) to 10.

This is because our attribute is not private and hence it can be modified outside the class.

To make an attribute private in a class, you will have to add two underscores at the beginning of an attribute.

Here is the correction of TossDie.py program

As I ran the program above, Here’s my result:

Note:

Even with this line of code myDie.side_up = 10, the attribute side_up doesn’t change to 10.

Making your attributes private prevents the value of the attribute from being modified outside the class.

Python Special Class Methods

A method is any function defined in a class. A special Python class method starts and ends with two underscores. There are many special class methods but I will only look at the one I feel that’s so important.

__init__() constructor method:

To explain the __init__() method, open a new editor and copy the below code snippet. Save it as my_info.py

Here’s the output of the program above.

In the my_info.py program, there are two methods which are:

  1. __init__() method
  2. getStatus()

Did you noticed that I didn’t call the __init__() method but “Godson is 21 years old.

” was an output when I ran the program.

Also I had to call the getStatus() method (data1.getStatus()) to print out “Godson is Single”

Let me further explain this to you.

The __init__ method is also called a Python class constructor or an initializer method; because it runs automatically when the programmer creates an instance of the class. It is the first method defined in a class.

Any object you make from a class is called an instance. In the my_info.py program, data1 is an instance to the class MyInfo.

Note:

When defining a method in a class, start with the parameter “self”. The other parameters such as name, age including self are known as instance variables.

Click here to know the function of the self variable.

As I’ve explained before print(data1.__dict__) returns a dictionary in which the key is the attribute and the value associated to the attribute. This is why we had {‘name’: ‘Godson’, ‘age’: 21, ‘status’: ‘Single’} as an output.

What’s the difference between method and function?

When defining a method, the first argument or parameter passed is self. The self parameter is not required when defining a function.

Note:

All the parameters passed in a method are known as instance variables.

__str__() method

Whenever you want to create a string representation of an object, you use the __str__ method. It is similar to the str() Python built-in function. It is advisable to include this method in your program as it aids in debugging. It would be nice to always know the state of an object after running a new method.

To illustrate the __str__() method, I will write a simple program to buy bitcoins and the program name is bitcoin.py

The __init__ method initializes the attributes __btc_value and  __my_btcs with 100 and 0 respectively. __btc_value represents the value of bitcoin and __my_btcs represents the number of my bitcoins.

The method buy_bitcoin buys bitcoins, the sell_bitcoin sells bitcoins and the available_bitcoin returns the number of bitcoins available.

The __str__ method returns a string representation of the object’s state.

The next program bitcoin_demo.py imports the bitcoin class module and creates an instance of the Bitcoin class.

Whenever you want Python to call the __str__ method just use this syntax,

print(class instance)

In the bitcoin_demo.py program, the class instance is “my_wallet”.

How to Modify Class Attribute’s Value

There are two ways to do this and they are:

  1. Directly modifying the attribute’s value
  2. Using a method to modify the value of the attribute

Directly Modifying the Attribute’s Value

This has been treated when I showed you how to make your class attributes private and I told you that this is important because it prevents the value of the attribute from being modified outside the class.

But any way, writing more codes will make you prefect so I will write a program in which an attribute’s value is modified directly.

Type the below code snippet in a new file editor and save it as student_info.py

Here is the output of the above code snippet

Explanation of the above code snippet.

In the init function the student_CGPA attribute is assigned a default value (4.0), that’s why I didn’t make it an instance variable. That is, the student_CGPA was not passed as parameter in the init function.

This line of code below,

student1.student_CGPA = 4.5

Basically tells Python to take the instance student1, find the attribute student_CGPA associated with it and set the value of that attribute to 4.5.

This is basically how you can modify an attribute directly.

Using a method to modify the value of the attribute

To change or check the value of an attribute of a class in a safe way, we use mutators and accessors methods.

A mutator is a method that changes the value of an attribute and an accessor is a method that returns the value of an attribute.

Take a good look at the code below:

Can you point out the accessor and mutator methods?

If you can’t, don’t feel bad.

Here are the mutator methods:

  1. set_employee_name
  2. set_employee_position

Here are the accessor methods:

  1. get_employee_name
  2. get_employee_position

It is advisable to start your mutator names with set and your accessor names with get.

How to Store Objects in a List.

Storing objects in list can make your work easier and present your objects in a nice way to a client.

I wrote a Python program(course_list.py)   to create objects which are my courses and store the objects(courses) in a list and display the list of courses.

Here is the code snippet:

Am sure if you go through the above code snippet thoroughly, you won’t have any problem.

But if you do, kindly ask me via the comment section.

Python Class Inheritance

Inheritance simply means that a new class known as the child class inherits all the attributes and methods of an existing class known as the parent class. The child class can also define new attributes and methods for itself.

Some programmers refer the child class as a sub class and the parent class as the super class. They all mean the same.

The picture below is a classic programming joke on object oriented programming.

Funny programming joke on object oriented programming

Alright, let’s do some example.

I inherited some things from my dad, like complexion, height, emotions and even course of study.

So I will write a simple program to illustrate me inheriting these features from my dad.

I would love that you observe the below code snippet. The program is saved as class_inheritance_example.py in case you downloaded the code snippet of this tutorial.

When you run the program above, you will get this.

The Dad class has the following functions/methods which are complexion, height, and field of study. This methods return the following values respectively, fair, tall, and chemical engineering.

This line of code, “class Godson(Dad):” means that Godson is a subclass of Dad. That is, it tells Python that the Godson class( subclass) should inherit all the methods of the superclass(Dad).

Whenever you define a class, you must include at least one method. Since I don’t have any method to include in the Godson class, I used the pass statement. This prevents error when I run the program.

The pass statement can also be replaced with a method. The code snippet below is a modification of the class_inheritance_example.py program.

when the run the code snippet above, you will get this.

Godson hair color is white

Multiple Inheritance

A sub class can inherit the methods of two or more classes.

To illustrate this, I will add another class Mom to the class_inheritance_example.py program.

Here is the new modification. The program below is saved as multiple_class_inheritance.py in case you downloaded the code snippet for this tutorial.

When you run the above code, you will get the following result.

This line of code “class Godson(Dad,Mom):” simply tells Python that the Godson class(sub class) should inherit all the methods of the Dad and Mom class which are the super class.

Class inheritance with the __init__ method

So far we have seen a classes without the __init__ method inheriting the methods of a super class.

What if the class has the __init__ method?

I will illustrate this with an example. The program below is saved as modified_class_inheritance.py in case you downloaded the code snippet for this tutorial.

Like as I’ve said before,  “class Godson(Dad):” tells Python that the sub class(Godson) should inherit all the methods in the super class(Dad).

Take a look at this line of code below:

def __init__(self,complexion,field_of_study,hair_color):

Did you noticed that the instance variables(complexion and field_of_study) of the class Dad were included.

This is because I inherited all these from my dad. The last parameter(instance variable) which is hair_color was not inherited from my dad. In fact, that’s what makes me different from my dad.

Also take a look at this line of code below:

Dad.__init__(self,complexion,field_of_study)

The above code simply calls the Dad’s __init__ method. The self parameter is included because when the method is executed, it initializes the complexion, field_of_study attributes.

How to Override a Method from the Super Class

In Python, you can override a method in the super class that no longer describes the sub class.

To do this, simply define a method in the sub class with the same name as the method in the super class that you want to override.

I will illustrate how to override a method from the super class in the code snippet below. The program below is saved as overriding_example.py in case you downloaded the code snippet for this tutorial.

when you run the above code snippet, you will get this as a result:

Godson is fair in complexion.

Godson is an entrepreneur

Am sure you won’t have any problem understanding the overriding_example.py program.

We are able to override the method of a super class because of a simple concept known as Polymorphism.

The idea behind polymorphism is that,

a method in a sub class can have the same name as the name in the super class.

Object-Oriented Programming Terms in Python.

Below is the definition of technical terms used in this tutorial.

We have come to the end of this tutorial.

Please kindly tell others how awesome this tutorial is via Twitter. It will mean a lot to me. Thanks.Click To Tweet

Please if you have any questions or contributions, please leave it in the comment section.

Thanks for reading. If you enjoyed this tutorial, hit the share button. It would mean a lot to me and it helps other people to see the tutorial.

About the author

Rapture Godson

I am Godson; the brain behind Cool Python Codes. On this blog, I will make Python much fun and very practical. My up-to-date tutorials are based on my studies and they are very easy for you to understand.

10 Comments

Leave a Comment