Thursday, August 5, 2021

Python for Beginners: Classes in Python

If you are a beginner in python programming, you must be knowing about primitive data types like integers, floating point numbers, strings and complex numbers. Also, you might have learnt about in built data structures like python dictionary, list, tuple and set. In this article, we will learn to create complex data types to store information about real world objects using classes in python.

What are classes in Python?

As we know that python is an object oriented programming language, we can create objects to represent real world objects in python. 

An object in python is a collection of attributes which identifies the real world objects.

For example, if we try to depict a cuboid as an object in python, we will specify the value of length, breadth and height of the cuboid and will define its properties such as surface area,weight and volume.

To define the properties of objects as for the cuboid, we use classes. Thus, classes are blueprints for objects in python and classes determine the attributes and functionalities of an object. 

In our example of cuboid, a class will be a construct which will define the length, breadth, height, surface area, weight and volume of the object. A class defining attributes of a cuboid will be defined as follows.

class Cuboid:
    #code for defining constructor
    #codes to define methods

How to create objects using classes in Python?

Classes are just a blueprint for any object and they cannot be used in a program. To create the object defined by the class, we use the constructor of the class to instantiate the object. Due to this, an object is also called an instance of a class.

The constructor of a class is a special method defined using the keyword __init__(). The constructor defines the attributes of the object and initializes them as follows.

class Cuboid:
    def __init__(self):
        self.length=0
        self.breadth=0
        self.height=0
        self.weight=0

We can also create a constructor which takes the attribute values as input parameters and them initializes them as follows.

class Cuboid:
    def __init__(self, length, breadth, height, weight):
        self.length = length
        self.breadth = breadth
        self.height = height
        self.weight = weight

Class attributes

Attributes of objects are private to any instance of a class. Unlike this, class attributes are attributes of a class itself and they are shared by every instance of the class. 

The class attributes are declared outside all the methods and constructor just below the class header as follows.

class Cuboid:
    name = "Cuboid"

    def __init__(self, length, breadth, height, weight):
        self.length = length
        self.breadth = breadth
        self.height = height
        self.weight = weight

In the above code, we have defined a class attribute “name” for the Cuboid class.

Defining Methods in a class

To define the properties and functionalities of an object, we define methods in a class. The methods are functions defined inside a class which are supposed to perform a specific task and give some output.

For Example, the methods to determine the surface area, volume and density of the cuboid from length, breadth, height and weight of the cuboid can be defined as follows. 

class Cuboid:
    name = "Cuboid"

    def __init__(self, length, breadth, height, weight):
        self.length = length
        self.breadth = breadth
        self.height = height
        self.weight = weight

    def volume(self):
        x = self.length
        y = self.breadth
        z = self.height
        v = x * y * z
        print("The volume is:", v)

    def density(self):
        x = self.length
        y = self.breadth
        z = self.height
        v = x * y * z
        d = self.weight / v
        print("Density is:", d)

    def surface_area(self):
        x = self.length
        y = self.breadth
        z = self.height
        s = 2 * (x * y + y * z + x * z)
        print("The surface area is:", s)

Having defined all the attributes and methods of a class, we can use it in our python program to instantiate an object and use them as follows.

class Cuboid:
    name = "Cuboid"

    def __init__(self, length, breadth, height, weight):
        self.length = length
        self.breadth = breadth
        self.height = height
        self.weight = weight

    def volume(self):
        x = self.length
        y = self.breadth
        z = self.height
        v = x * y * z
        print("The volume is:", v)

    def density(self):
        x = self.length
        y = self.breadth
        z = self.height
        v = x * y * z
        d = self.weight / v
        print("Density is:", d)

    def surface_area(self):
        x = self.length
        y = self.breadth
        z = self.height
        s = 2 * (x * y + y * z + x * z)
        print("The surface area is:", s)


myCuboid = Cuboid(1, 2, 4,4.5)
myCuboid.density()
myCuboid.surface_area()
myCuboid.volume()

Output:

Density is: 0.5625
The surface area is: 28
The volume is: 8

Conclusion

In this article, we have studied the concept of classes in python. We have also seen how to implement the constructors and methods in a class using the example of a cuboid. We can also write the programs used in this article with exception handling using python try except to make the programs more robust and handle errors in a systematic way . Stay tuned for more informative articles.

The post Classes in Python appeared first on PythonForBeginners.com.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...