Classes

Classes

We explored OOP in an earlier blog and we touched on classes, so in this blog let’s explore classes in a greater length. Classes provide a way to package together data types and methods or functions, that are interrelated.

Let’s consider the syntax of creating a class:

  • class <class_name>:

….–code—

class First_Class:
    variable1 = "Verification Master"

test_1 = First_Class()  # We created an object of our class

print("Within the class we have stored:", test_1.variable1)

We run this we get the output:

We created a class called “First_Class” and created a variable with text in it. To instantiate or create an object of a class, all we need to do is follow the syntax:

  • <object_name> = <class_name>

We then used the dot operator to access the variable we defined inside our class.

We can also modify the variable of an object in the following way:

test_1.variable1 = "Verification_Master" # replacing space with _

Let’s modify the above code by adding a function and an input.

class First_Class:

    def add(self, number):
        return number + 100

user_input = int(input("Enter a number:"))

test_1 = First_Class()  # We created an object of our class
result = test_1.add(user_input)

print("Result is:", result)

We get the result:

 

We have added a function called “add” and it will add 100 to a number. We then collected the user’s input and then passed that as the argument for the function we created. To access a function all we have to do is use the dot operator similar to how we access a variable.

Let’s now see an example of how we can use a class efficiently.

class Animal:

    animal_type = "carnivore"  # this is shared with all instances

    def __init__(self, name):
        self.name = name  # this is unique for each instance

animal_1 = Animal('Tiger')
animal_2 = Animal('Lion')

print("Animal 1 is a:", animal_1.animal_type)
print("This animal is a:", animal_1.name)
print("")
print("Animal 2 is a:", animal_2.animal_type)
print("This animal is a:", animal_2.name)

When we run this, we get:

We created a class called “Animal” with a variable that conveys what type of an animal it is, which in this case is a “carnivore”. Recall what we discussed about the __init__() function, it gets called when we create an instance of a class. Here the __init__() function takes an argument which is the name of the animal.

We can also observe that for each instance of the class, we have the variable “animal_type” as the same value, but the variable “name”, has the value we passed as an argument when we instantiated each of the objects.

NOTE: We will cover more about the __init__() function, in detail in a later blog.

There are ways in which we can inaccurately use a class, thus, consider a faulty way of using classes:

class Fruits:

    fruit_quality = []  # shared variable

    def __init__(self, name):
        self.name = name

    def give_review(self, quality):
        self.fruit_quality.append(quality)


fruit_1 = Fruits("Apple")
fruit_1.give_review("Exceptional")

fruit_2 = Fruits("Mangoes")
fruit_2.give_review("Rotten")

print("Quality of", fruit_1.name, "->", fruit_1.fruit_quality)

This gives us the output:

Instead of printing all the reviews of an apple, this also printed the review given for mango as well. The correct way of using a class would be:

class Fruits:

    def __init__(self, name):
        self.name = name
        self.fruit_quality = []  # makes it unique for each instance

    def give_review(self, quality):
        self.fruit_quality.append(quality)


fruit_1 = Fruits("Apple")
fruit_1.give_review("Exceptional")

fruit_2 = Fruits("Mangoes")
fruit_2.give_review("Rotten")

print("Quality of", fruit_1.name, "->", fruit_1.fruit_quality)
print("Quality of", fruit_2.name, "->", fruit_2.fruit_quality)

This will accurately print the review of each fruit.

What have we learned?

  • What is the syntax of creating a class?
  • How do we create an object of a class?
  • How to use an object to access a variable and a function of a class?
  • How do we modify a variable of an object?
  • How to create a variable that is unique to each instance?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments