Piton

Python Classes

Python Classes

Python is one of the multiuse high-level programming languages. It is an object-oriented programming language. The main difference between the procedural and object-oriented programming languages is that we cannot create the classes in procedural programming languages. The main focus of procedural languages is on creating functions, and variables for performing the task whereas, in object-oriented programming languages, our main concern is to create objects and use them for performing our tasks. A class is simply a blueprint that contains functions and variables. A class is like a real-life classroom of any institute. It contains some chairs, tables, desks, a projector, walls, etc. base on all these components; we build a classroom. All these components are the variables and functions in a class, and a classroom is an object. The Python classes and objects are explained in this article.

Creating a class in Python

In Python, a class is created by using the class keyword. The keywords are used for special purposes.  A class in Python contains the attributes and functions. The attributes are the variables. The attributes could be public or private.  The private variables in Python class start with the double underscore (__).

Let's create a person class that has a name, age, and gender as an attribute. The attributes of a class can be called or accessed by the class name using a dot.

class person:
name = "Kamran"
age=25
gender="male"
#printing the person name
print(person.name)
#printing the age of the person
print(person.age)
#printing the gender of the person
print(person.gender)

Output

All the variables are public in the above-given code.

When a class is created, a new class object with a class name is created.

Creating functions in Python class

Functions in Python class are created by the def keyword.  A function is a code block that accomplishes a particular purpose. For example, if we want to calculate the sum of two numbers, we can write a separate function for this purpose. Now, we would like to add a couple of functions in the class.

class person:
name = "Kamran"
age=25
gender="male"
#a function to set the person name
def setname(self,name):
self.name=name
#a function to set the person age
def setage(self,age):
self.age=age
#a function to set the person gender
def setgender(self,gender):
self.gender=gender
#a function to get the person name
def getname(self):
return self.name
#a function to get the person age
def getage(self):
return self.age
#a function to get the person gender
def getgender(self):
return self.gender

We have created the getter and setter functions for the attributes. The setter functions set the value of the attribute, whereas, the getter function returns the value of the attribute to the calling object. The self parameter is used to define the context of the class or object. The attributes and the objects can be accessed by using the self keyword. The self keyword binds the objects, attributes, and functions of a class. I hope now you are familiar with creating the classes, attributes, and functions in Python. Now let's move on and create the objects.

Creating objects in Python

An object is the instance of the class. The object in Python is used to access the variables and function. An object have all the properties of a class because it represents the class. An object has to be defined with the class name as it is the copy of it. The syntax of object creation is as follows:
object = class()

For the person class, the object will be created in this way:
kamran = person()

Now the class attributes and functions can be accessed using the object name. Let's do it in our Python script.

class person:
name = "Kamran"
age=25
gender="male"
#a function to set the person name
def setname(self,name):
self.name=name
#a function to set the person age
def setage(self,age):
self.age=age
#a function to set the person gender
def setgender(self,gender):
self.gender=gender
#a function to get the person name
def getname(self):
return self.name
#a function to get the person age
def getage(self):
return self.age
#a function to get the person gender
def getgender(self):
return self.gender
#creating the object of person class
kamran = person()
#accessing the variable
kamran.name="Kamran Awaisi"
#accessing the function
print(kamran.getname())

Output

Everything worked out pretty smooth, which means we have no errors.

The initialization function in Python class

The initialization function is used to initialize an object at the time of object creation. In most object oriented programming languages, first initialization of the object is referred to as constructor and can be used with or without arguments passed through them in the code. It starts with the double underscore (__). All the function that start with the double underscore in Python class, have some special meaning. The name of the initialization function is __inti__ in Python. Let's create an initialization function in person class to initialize the person name, age, and gender at the time of object creation. The initialization function takes self as parameter to get the context of object.

class person:
#creating private variables
__name = ""
__age=0
__gender=""
#initilization function
def __init__(self,name,age,gender):
self.__name=name
self.__age=age
self.__gender=gender
#a function to set the person name
def setname(self,name):
self.__name=name
#a function to set the person age
def setage(self,age):
self.__age=age
#a function to set the person gender
def setgender(self,gender):
self.__gender=gender
#a function to get the person name
def getname(self):
return self.__name
#a function to get the person age
def getage(self):
return self.__age
#a function to get the person gender
def getgender(self):
return self.__gender
#creating the object of person class
#passing the value of name, age, and gender
kamran = person("Kamran",12,"male")
#printing the name
print("The name is:",kamran.getname())
#printing the age
print("The age is:",kamran.getage())
#printing the gender
print("The gender is:",kamran.getgender())

Output

Conclusion

The Python is an object-oriented programming language that supports the creation of classes, and objects. A class contains the attributes, and functions. The attributes are variables that store the information, whereas functions are used to perform a specific task. We have learned the usage of classes and objects with the help of appropriate examples.

Mouse left-click button not working on Windows 10
If you are using a dedicated mouse with your laptop, or desktop computer but the mouse left-click button is not working on Windows 10/8/7 for some rea...
Cursor jumps or moves randomly while typing in Windows 10
If you find that your mouse cursor jumps or moves on its own, automatically, randomly while typing in Windows laptop or computer, then some of these s...
How to reverse Mouse and Touchpads scrolling direction in Windows 10
Mouse and Touchpads not only make computing easy but more efficient and less time-consuming. We cannot imagine a life without these devices, but still...