Object uses variables to store data called instance variables. variables can also be created in class context, those variables are called Class variables. Variables(fields) and methods are attributes of class.
Along with variables, class also contain methods. In OOP terminology , instance variables are also known as data members and methods are also
known as member functions.
Class is created using keyword class and fields, methods are added in indented block.A sample class can be created like this.We will see in detail about object included in Employee class while dealing with inheritance in python.self identifier and __init__():- Before going delve into actual class creation, let's understand importance of self and init() in python.
- It is the self identifier which differentiate class methods from normal python functions. It is mandatory to include self as first parameter list in class methods(member function).self identifies the instance upon which a method is invoked.Please note that, instead of "self" we can use any other name as first parameter in class method, however it is highly recommended to use "self" for the same. When we call any method in object context, we do not pass any thing for self parameter, python handle internally it and pass object reference to it.
- __init__() method in python serves as constructor as in other language for creating instance of class.When an instance of class is created then python internally execute __int__() method and initialize member fields(instance variables).
Lets walk through the code lines of above class: _basicSum and _allowance are class variables (shared by all instance of class). After that __init__ (self, _name,_employeeId, _position) is defined with first parameter self and other three are instance members. When we create instance of Employee this _init_(...) is executed automatically and member variables are initialized with value passed and default value 0 for _leaveBalance.We have getter method for these member variables and followed by two member function to allocate leave and calculateSum corresponding for leave. We can spot that self is used to access member variables corresponding to the object and class variables is accessed via class name as it is carries in other language like java.
Now we will following operation in sequence : 1. create instance of Employee class 2. Allocate leave to new Employee instance. 3.Calculate sum total for leaveBalance.
Create instance of Employee class like this in python terminal :
>>> employee1 = Employee('Nikhil',007,'SSE') # employee1 reference an instance of Employee
>>> employee2 = Employee('Ranjan',99,'TL') # employee2 reference an instance of Employee
Allocate leave to both(employee1 and employee2) Employee instance like this:
.>>> employee1.allocateLeave(12) # 12 leaves assigned to employee1
>>> employee2.allocateLeave(15) # 15 leaves assigned to employee2
Calculate sum total for these two instances:
>>> employee1.getTotalSumForLeaveBalance() #No value passed for self ,python does it implicitly.
Above sequence of events for instance employee1 can be summarized by sequence diagram:
|Sequence diagram for creating employee1 instance and calculating sum total|
- Every object refers to it’s class via the self.__class__ attribute. So, instead of accessing class variable like Employee._basicSum , we can use self.__class__._basicSum.
- __class__ is a built-in attribute of every class instance. It is a reference to the class that self is an instance of.
- All class members (including the data members) are public and all the methods are virtual in Python.
- Private name mangling: When an identifier in class definition begins with two or more underscore characters and does not end in two or more underscores, it is considered a private name of that class.
Sample example using class and Object:-
Previous: Iterators and Generators in python Next : Method overloading and Operator overloading