Classes & Object-Oriented Programming in Python

vaatiesther - Jul 24 '23 - - Dev Community

Object-Oriented programming is a programming paradigm that allows you to create programs based on classes and objects. Look at the following code,

What is a Python class?

A python class is similar to a function but it uses the class keyword. The syntax for a class looks like this:

class Person:
    '''This is a class named  person'''
    pass
Enter fullscreen mode Exit fullscreen mode

When naming classes, always follow PEP 8 style guide, which says that class names should be capitalized like this:

class ClassName:
    pass
Enter fullscreen mode Exit fullscreen mode

What is an Object in Python

Everything in python is an object. Since everything is an object, we can create an object from classes, a process known as instantiating.

house = Person()
Enter fullscreen mode Exit fullscreen mode

Note the additional parenthesis at the end. You will get the following output if you print type(house):

<class '__main__.Person'>
Enter fullscreen mode Exit fullscreen mode

Class Variables

A class will have variables as well. Variables are like attributes and are shared by all instances. For example, suppose the person class has a variable name, as shown below:

class Person:
    '''This is a class named  person'''
    name = 'Joe doe'
Enter fullscreen mode Exit fullscreen mode

Every instance of the Person class will have the name i.e Joe doe as shown below:

person1 = Person.name
person2 = Person.name
print(person1)
print(person2)
Enter fullscreen mode Exit fullscreen mode

The output will be:

Joe doe
Joe doe
Enter fullscreen mode Exit fullscreen mode

Class Methods

Classes can also have methods and are defined inside the class. For example, a person can drive a car; let’s define a method in the Person class.

class Person:
    '''This is a class named  person'''
    name = 'Joe doe'

    def can_drive(self):
        print('{} can drive all types of vehicles'.format(self.name))

person1 = Person()
print(person1.can_drive())
Enter fullscreen mode Exit fullscreen mode

In the code above, we define a method that returns a string and then calls the method on a Person instance. Convention requires that the first argument in class methods be self. Here self refers to the object
The output will be:

Joe doe can drive all types of vehicles
Enter fullscreen mode Exit fullscreen mode

You can also add arguments to the class methods in addition to the self-argument. Let’s add another method with some arguments to our person class.

class Person:
    '''This is a class named  person'''
    name = 'Joe doe'

    def can_drive(self):
        print('{} can drive all types of vehicles'.format(self.name))

    def calculate_bmi(self, weight, height):
        return weight/height
Enter fullscreen mode Exit fullscreen mode

In the code above, we add a method calculate_bmi which takes two additional arguments, weight and height, and returns the BMI, which is weight divided by height.

Now when you call the method, you need to pass in values to the method, as shown below:

person1 = Person()
print(person1.calculate_bmi(62,161))
Enter fullscreen mode Exit fullscreen mode

The output will be:

0.38509316770186336
Enter fullscreen mode Exit fullscreen mode

Dunder methods

Dunder methods are methods that are represented by double_underscore on either side, and they include:

  • __init__()
  • __str__()
  • __repr__()

The init() method initializes a new object. It is also known as a constructor and is defined as follows:

class Person:
   def __init__(self)
        pass
Enter fullscreen mode Exit fullscreen mode

If you defined any arguments in the init() method, they would be available every time the class is instantiated.

class Person:
    def __init__(self, name):
        print('{} belongs to the VIP class'.format(name))
Enter fullscreen mode Exit fullscreen mode

Let's instantiate a person object:

person_vip = Person('john')
print(person_vip)
Enter fullscreen mode Exit fullscreen mode

We will get the following output:

john belongs to the VIP class
<__main__.Person object at 0x7f87129324f0>
Enter fullscreen mode Exit fullscreen mode

__repr__() is another dunder method used to give a more meaningful representation of a class. The __repr__ () method usually returns a string. Let’s say you want to get information about the Person class below

class Person:
    def __init__(self, name):
        self.name = name
person = Person('John')
print(person)
Enter fullscreen mode Exit fullscreen mode

The output of the code above will be:

<__main__.Person object at 0x7fb0b38642e0>
Enter fullscreen mode Exit fullscreen mode

This doesn’t give any meaningful information about the class. Let’s add a __repr__() method to the class Person above.

class Person:
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return self.name


person = Person('John')
print(person)
Enter fullscreen mode Exit fullscreen mode

The output will be:

John
Enter fullscreen mode Exit fullscreen mode

Object-Oriented Programming

The four pillars of object-oriented programming (OOP) are:

  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Inheritance

Inheritance is a concept where classes inherit the properties of another class. Suppose we wanted to create a database of all the people in a school. A school can have students, teachers, non-teaching staff, etc. We would then create different classes for the category, as shown below.

class Student:
    pass
class Teacher:
    pass
Enter fullscreen mode Exit fullscreen mode

Suppose we wanted to create a method contact which is shared by all which returns the contact details.

class Student:
    pass
class Teacher:
    pass
Enter fullscreen mode Exit fullscreen mode

Rather than writing the same method in both classes, we can create a parent class, define all the methods that the two classes share, and then use inheritance to inherit the methods. here is how the parent class would look like.

class Person: 
  def contact(self): 
    print("print contact details")
Enter fullscreen mode Exit fullscreen mode

The student and Teacher classes now automatically inherit the properties of the Person class, as shown below.


class Person: 
  def contact(self): 
    print("print contact details")

class Student(Person):
    pass

class Teacher(Person):
    pass
Enter fullscreen mode Exit fullscreen mode

If we instantiate a student and teacher object, we will discover that the classes have inherited the contact method.

student1 = Student()
teacher1 = Teacher()
print(student1.contact())
print(teacher1.contact())
Enter fullscreen mode Exit fullscreen mode

The output will be:

print contact details
None
print contact details
None
Enter fullscreen mode Exit fullscreen mode

Polymorphism

Polymorphism is another pillar of Object-Oriented programming where a method is applied to different objects. Suppose you have an Animal class that defines a method called is_branded, as shown below

class Animal():
    def is_branded(self):
       print("This animal is branded")
Enter fullscreen mode Exit fullscreen mode

Another class can have the same method i.e, is_branded which returns something different as shown below

class Product():
    def is_branded(self):
       print("This product is branded")

Enter fullscreen mode Exit fullscreen mode

The two classes above, i.e., Product and Animal, have no relationship but share a common method which means something different when applied to each class. This concept is called polymorphism

Abstraction

Abstraction is another pillar of OOP. To abstract means to hide. Abstraction refers to the process in which programs hide the underlying functionality and can be used without the need to know what is happening behind the scenes

For instance, consider the example we use above.

class Person: 
  def contact(self): 
    print("print contact details")

class Student(Person):
    pass

class Teacher(Person):
    pass
Enter fullscreen mode Exit fullscreen mode

To make the Person class an abstract, it will inherit from Abstract Base Class(ABC), then apply the abstract method to the contact() method as shown below.

from abc import ABC,abstractmethod
class Person(ABC): 

  @abstractmethod
  def contact(self): 
    print("print contact details")

class Student(Person):
    pass

class Teacher(Person):
    pass

student1 = Student()
teacher1 = Teacher()
Enter fullscreen mode Exit fullscreen mode

Now the animal class cannot be instantiated. Let's instantiate it to see the result.

student1 = Student()
TypeError: Can't instantiate abstract class Student with abstract method contact
Enter fullscreen mode Exit fullscreen mode

To make the error go away, we have to add the contact() method to the student and Teacher class as shown below.

from abc import ABC, abstractmethod
class Person(ABC): 

  @abstractmethod
  def contact(self): 
    print("print contact details")

class Student(Person):
    def contact(self): 
        print("print student contact details")

class Teacher(Person):
    def contact(self): 
        print("print teacher contact details")
Enter fullscreen mode Exit fullscreen mode

Encapsulation

Encapsulation is the act of enclosing data; you can only use the data in the enclosed unit. While other programming languages perform encapsulation with public, protected, and private methods, Python doesn’t have the same. In Python, we prefix the variable with a single underscore if you need to define a private variable.

class Person:
    def __init__(self):
        self.__name = 'john doe'
Enter fullscreen mode Exit fullscreen mode

For a protected variable, you prefix with a single underscore.

class Person:
    def __init__(self):
        self._name = 'john doe'
Enter fullscreen mode Exit fullscreen mode

Conclusion
This tutorial has covered how to write Python classes from scratch, and how to define methods and object-oriented programming in Python. These are the fundamental concepts will make you a great programmer.

. . . . . . . . . . .
Terabox Video Player