Python Class- Creating, Instantiating and Using in a Program

Python offers a huge repository of inbuilt data types and compound data types like lists, strings, arrays etc. As a programmer you create User Defined Data type called Python Class. It adds flexibility and reusability in your projects. A class is a template defined to create objects that can have attributes and methods. Classes make programming easier by letting the developer understand the problems as collection of interacting and dependent classes.

Python Class Creation

A python class is created with the keyword class.  It has following syntax

class class-name:

            [Class level Attributes]

            def __init__(self, argument list):                   #This is constructor header

                        constructor body to initialize object attributes

            def method-name(self, argument list):         #This is method header

                        method body to define functionality of the method

Instantiating of Python Class

A Python class is instantiated in two ways. By assigning the values to the attributes or by calling the constructor function

By assigning the values to the attributes

 Class-name.attribute-name=value

Example:             bird.category=”Parrot”

By calling the constructor

Object-name=class-name(initial values)

Example:             Pr=bird(“parrot”)

Calling the Class Methods

Python class methods are called by qualifying the name of the method with the object name that is already created and instantiated. You can call a class method only with an object name.

Pr.addAct(“Hides”)

Example of Python Class and Creating Objects

class bird: 	#Name of the class
    def __init__(self, category):   #Constructor header
        self.category = category      #attribute
        self.actions = []		# an array attribute to store a bird’s actions
    def addAct(self, actions):		#Method header
        self.actions.append(actions)	#Method Body adds action of a bird to the existing actions

This will create a user defined class called bird.

This class define constructor that accepts three arguments. The first argument is always self indicating the class object for which the constructor is called. Remaining arguments are the values to be assigned to the objects attributes.

Note: You can add the following code at the end of the python file. You can also run these commands on command prompt after the code to create class has been executed

sp=bird("sparrow") # Creating three bird categories
cr=bird("crow")
pr=bird("parrot")
sp.addAct('Flies')  #Adding actions of the bird categories
sp.addAct('Chirps')
sp.addAct('Preens')
cr.addAct('Flies')
cr.addAct('caws')
cr.addAct('bites')
pr.addAct('Flies')
pr.addAct('mimics')
pr.addAct('laughs')
pr.addAct(‘Hides’)

Output

Python Class Example

So, you can see that creating Python Class as user defined data types is very easy. You must be aware of the OOPS concepts to implement classes in your Python programs. To know more about Python Classes you can read here

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *