Object oriented programming languages make coding very simple which is why Python tool provides support for classes and objects. In this chapter, we will have a look at how object oriented coding can be done in Python. For those who are new to the OOPs, the following information about the basics will be helpful.
Class: A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
Object: A unique instance of a data structure that’s defined by its class. An object comprises both data members (class variables and instance variables) and methods.
A class is the basic building block for Python. It is created to define all the parameters that are required to create an instance of that class. It logically groups all the functions related to an entity. Technically, a class can be created in any manner, but while programming it is preferable to assign one class to one real world entity.
For example, for a banking software we will create customer class, employee class, loans class, salary, class etc. So, that we can go ahead with programming in a systematic manner.
A class or an instance variable that holds data related to the class and its objects is called a data member. A variable defined inside a method of a class is called an instance variable and a variable defined outside the methods of a class are called class variables. It is also possible to derive one class from the other and the process is called inheritance. Functions defined within a class are also called methods.
A class is defined in the following manner:
class Class_name: code(consisting of functions, variables, etc) The object of a class supports attribute reference and instantiation. In case of attribute reference the attributes can be referred in the following manner: object_name. object_attribute.
Class instantiation on the other hand, requires function notation. You will have to first create an instance:
object_name=class_name() class_name() creates an instance of the class and it is assigned to local variable object_name
Now, you cannot move further without understanding the importance of ‘ self ’. In python, the first argument of every function in a class always refers to its current instance. ‘ Self ’ is the new object that is created and whose function is called. The value of ‘self ’ attribute is not passed explicitly to a function.
So, with this information in mind, let’s analyze the code written above:
firstobj=funcx() # creates object of class funcx()
firstobj.f(7) #calls function f() in the class where x=7, self is current state of
# A value of ‘ 7 ’ is assigned to ‘ x ’ for firstobj
A python class may or may not have to “define a __init__()” function. Sometimes, when there is a need to create an object that are customized to an initial sate , the developer may define a __init__() function in the class.
Instance objects are created by calling the class name and passing the arguments that are required by its __init__ function.
You can access the attributes of an object by using a dot operator and if you want to access a class variable, then you will have to use a dot operator with the class name.
Common ways to access attributes are given below:
-Create an attribute for an object
objectname.attr = value
OUTPUT : 7.0
-To check if an attribute exists or not
>>> hasattr(location1, ‘distance’)
OUTPUT : True
-Access an attribute of an object.
getattr(obj, name[, default])
>>> getattr(location1, ‘distance’)
OUTPUT : 7.0
-Set an attribute or create it if it does not exist
OUTPUT : 10.1
–Delete an attribute.
The delattr(obj, name)
OUTPUT : AttributeError: Point instance has no attribute ‘distance’
Enroll for Instructor Led Live PYTHON TRAINING
Get Updates on Tech posts, Interview & Certification questions and training schedules