Classes and Objects in OOP With Python, classes in python, python classes, python objects, objects in python python classes, classes in python, python class, class python

Hello pupil! We hope you are doing well with object-oriented programming with Python. OOP is an extremely important topic in programming, and the good thing about Python is that the concept of OOP can be implemented in it. In the previous lecture, we made a basic introduction to all the elements that are used in the OOP, and right now, we are moving forward with the detail of each concert with examples. The imperative nature of the OOP is useful for us because it uses the statements to change the programming state, and in this way, the working of the code becomes easy and effective. To use the applications of Python, the imperative nature of OOP will give us the tools we need to get the perfect results by using our creativity. We will prove this in just a bit, but here are the highlights of the topics that will be discussed. 

  • How can you describe the OOP by using an example?

  • Why are classes important in OOP?

  • Discuss the example and syntax of code when we use classes in OOP.

  • What are objects?

  • Give us an example where multiple objects are made if one class and the information are given by the user.

  • Discuss the basic features of objects in OOP.

  • How do you classify the attributes of objects?

  • What are the behaviours in OOP?

All of these are important to learn so pay attention to each and every concept and if you have any confusion, you can contact us directly.

Classes and Objects in OOP With Python, classes in python, python classes, python objects, objects in python python classes, classes in python, python class, class python

Example of the OOP with Python

Before going into the details of the OOP, now is the time to discuss why we are emphasizing the importance of the OOP and how we explain it with Python. The whole concept will be clear with the help of the following example:

Think of a dog that will be taken as an example by a class. Here, we have taken a general example that is common in the real world. Think of all the characteristics of the dog that may be either physical or natural habits of the dog. The general class of dog may have many types, such as the breed of the dog. There are hundreds of breeds of dogs, and these are considered the basic members of the class "dog." 

Moreover, different dogs have different kinds of attributes such as their size, colour, skin type, age, and other characteristics that are said to be their instance attributes. On the basis of these attribute, the division of the dogs is done into different classes. 

Moreover, the way the dogs bark, their favourite food, and their daily routine make them more specific, and these are referred to as the methods of that particular class.

Classes and Objects in OOP With Python, classes in python, python classes, python objects, objects in python python classes, classes in python, python class, class python

Importance of Classes in Python OOP

The class is the basic unit, without which, the programmers may not think about the OOP in any way.  This is the prototype with which the whole OOP process is carried out. You can think of a class as the blueprint of the house (let's say). In this way, the information about the floor, walls, rooms, windows, doors, etc. In this way, the whole house is made by following this blueprint(class) whereas, the result, that is the house is the object. Let us prove all this information with the help of this example.

Syntax of the Class

The class is simply made in Python with the keyword “class” and the name that we want for our class:

Classes and Objects in OOP With Python, classes in python, python classes, python objects, objects in python python classes, classes in python, python class, class python

class myClass:

    # class definition

Here,

class=keyword

myClass= the name of the class given by the programmer.

In this way, with the help of simple lines, the creation process of the class is carried out. Now, the definition of the class is in the new line. To make a clear difference, a colon is used after the name of the class. You must keep in mind, the class name is a single word, no matter what the length of the name, and there must be no space between the name of the class. 

Example of Class in Python

In the example of the class given above, where we were talking about dogs. We are using the same example and will show you the result by using them in the example. To start with a simple example, the class with only three attributes. 

Code for Class

#declaring the class

class myDog:

#defining attributes

    def __init__(self, name, age):  

#using the attributes and storing them in the new names

        self.name = name

        self.age = age

#usign the stored name to print the results

tinku = myDog("Tinku", 2)

print("The name of the dog= ",tinku.name)

print("The age of the dog= ",tinku.age)

Output:

Classes and Objects in OOP With Python, classes in python, python classes, python objects, objects in python python classes, classes in python, python class, class python

Let's see what happens during this code:

  • First of all, we created a class with the class keyword, and this time, we named it “myDog” so you are providing the information about your dog( if you do not have it, just imagine it) and we are writing the particular features of that dog. So, the class is myDog, and all the other features are its attributes. 

  • In the next step, we are defining the characteristics with which we want to describe our dog. Here, name and age are the attributes. That is taken as one of the two arguments we are defining to use later.

  • The "self" keyword will tell us that name and age are the special attributes that we want to use here.

  • In the end, a new unit is formed, where this time, we are using the values of attributes instead of the names. The first step was only to give the base of what we wanted, and this time, we will assign values to these attributes. 

  • You can see, the name is in the form of a string, therefore we have used a double quotation. In this way, we do not have to specify the data type but just represent it in a specific way. 

  • In the end, it's time to print the result and to make the message clearer, we used the written message and then declared the value we wanted.

We can add more and more attributes to make the class clearer and store data about the dog, such as its colour, fur type, etc. This is a simple example to show you how the class works. 

Objects in Python OOP 

The workings of the class are not just limited to one product. This is the good thing about the OOP is, it provides us with the versatility to work in a different way with the same code. In the example given above, if more than one breed of dog is to be stored, then we will make the objects of the class "dog."

Classes and Objects in OOP With Python, classes in python, python classes, python objects, objects in python python classes, classes in python, python class, class python

Code:

To understand the objects in interesting ways, we are moving towards real-time applications where different objects are made and then used in a specific way. Let us take an example where we have different types of dogs with different ages, and the combination of the ages of the first two dogs is the age of the third dog. So here is the question that can be asked of you in the examination. 

Question:

Write the code to get the age of a dog that is the combination of two dogs and get the input from the user for it. 

Answer:

Classes and Objects in OOP With Python, classes in python, python classes, python objects, objects in python python classes, classes in python, python class, class python

#declaring the class

class myDog:

#defining attributes

    def __init__(self, name, age):  

#using the attributes and storing them in the new names

       self.name = name

       self.age = age

#using attributes in string message

    def dogInfo(self):

            print(self.name + " is " + str(self.age) + " year(s) old.")

#printing queries and getting input from user            

print("What is the age of happy")

AgeOfHappy=input()

print("What is the age of sunny")

AgeOfSunny=input()

AgeOfFancy=int(AgeOfHappy)+int(AgeOfSunny)

#using the inputs to guess the age of fancy 

print("Age of fancy= ",AgeOfFancy)

#printing results

happy = myDog("Happy", AgeOfHappy)

sunny = myDog("Sunny", AgeOfSunny)

fancy = myDog("Fancy", AgeOfFancy)

happy.dogInfo()

sunny.dogInfo()

fancy.dogInfo()

Output:

Classes and Objects in OOP With Python, classes in python, python classes, python objects, objects in python python classes, classes in python, python class, class python


Discussion:

This program has many interesting features, and these will become clear with the help of this discussion. 

  • At the beginning of the program, the class “myDog” is created so that we may give the same attributes to different objects. 

  • To declare the attributes we are using here the “def” keywords to define the keyword then the 

  • Attributes are mentioned so that we may use them later.

  • The next step is to define the message, in which we will use the attributes and get the result in the form of a message. Here, the point to notice is, we are using the string keyword for the "age,” but how can age be a string? Actually, we are using it in the string message, and if we declare the “int” here, the compiler, when it reaches this point at the output, will throw the error and not print the whole string. 

  • After that, the whole mechanism is used in which we are printing the message to input the age of the first two dogs and get the output in the form of integers. 

  • Here, if the user inputs a data type other than an integer, it will also result in an error. 

  • Both these ages are then added, and the result is saved in the “AgeOfFancy” variable.

  • The object we have just used to print the results.

So with the help of this discussion, we come to know that the object consists of the following features:

Attributes of Objects:

Each object has its own set of attributes, which are variables that store information about the object's current state. We can say that objects are made of attributes. Yet, there are different types of attributes in OOP, and the following are some of the most important characteristics of attributes:

  • Instance variables: These are defined within the class but are unique to every instance of the class.

  • Class variables: These are also defined within the class but are shared with all the instances.

  • Methods: These are used to manipulate the object’s state and are defined within the class to operate the attributes. 

  • Special methods: These are also referred to as the magic methods and these tell the compiler how objects will behave under certain conditions. These are denoted by the double score in the name as we have just used in the previous code of dogs. 

  • Inherited attributes: These are used to reuse the modularity in the code as these are inherited by a subclass or a superclass. So it makes working easy.

The Behaviour of Objects in OOP

When an object is created in OOP, it has its own set of behaviours, which are defined by the methods in its class. These methods can be used to change the state of an object. Another function of the behaviour is to carry out other operations related to the object's behaviour. For example, if we declare the object of a car, we might have methods such as:

  • start engine()

  • accelerate()

  • brake()

  • turn()

Classes and Objects in OOP With Python, classes in python, python classes, python objects, objects in python python classes, classes in python, python class, class python

These methods define the behavior of the car object and enable it to perform the actions that are expected of a car. These are just simple examples, and we can add more and more behaviors, just as a car can do hundreds of tasks.

Hence, in this lecture, we read a lot about objects and classes. There are different entities that are used in OOP, and objects and classes are the basics. We must know that objects and classes are used simultaneously, and for a better understanding, we learned the classes first and then examined where and how objects are being used. Moreover, different types of objects and fundamental knowledge about their pillars are discussed here. We have a lot of information to share with you, and you must have noticed that Python is now more complex but useful when we use the concepts of OOP. We have some mini Python projects to share with you in this tutorial, so stay with us for more programs.