top
April flash sale

Search

Python Tutorial

Object oriented programming is one of the various programming paradigms in which programming languages are classified. Early programming languages developed in 50s and 60s are recognized as procedural (or procedure oriented) languages. Object oriented approach was proposed to overcome limitations of procedure oriented programming language. So, we first try to understand these limitations.Procedural oriented approachEvery function is written in such a way that it can interface with other functions in the program. Data belonging to a function can be easily shared with other in the form of arguments, and called function can return its result back to calling function.This approach seems to be sound one, but as the scope of program becomes larger and complex, it begins to show signs of strain, eventually leading to failure. Prominent problems related to procedural approach are as follows:The top-down approach adapted by procedure oriented languages makes the program difficult to maintain. If a change in top-level function is required, it may lead to making cascaded changes in all lower level functions.Procedural approach uses a lot of global data items, which is undesired. Especially in big and complex programs with large number of functions, too many global data items would increase memory overhead.Computer program describes procedure of data processing. However, procedural approach gives more importance to process and doesn’t consider data of same importance and takes it for granted, thereby it moves freely through the program.Movement of data across functions is unrestricted. So long as number and type of arguments match, data from any function can be sent in any other function. This is not the reflection of real life scenario where there is unambiguous association of a function with data it is expected to process.Object oriented approachIn real world we come across, deal with and process objects, such as student, employee, invoice, car etc.. Objects are not only data and not only functions, but combination of both.Each real world object has attributes and behaviour associated with it.Attributes: Name, class, subjects, marks etc.. of studentName, designation, department, salary etc of employeeinvoice number, customer, product code and name, price and quantity etc in an invoiceRegistration number, owner, company, brand, horsepower, speed etc.. of carEach attribute will have a value associated with it. Attribute is equivalent to data.Behaviour:Processing attributes associated with an object.Compute percentage of student’s marksCalculate incentives payable to employeeApply GST to invoice valueMeasure speed of carBehaviour is equivalent to function. We can see that in real life, attributes and behaviour are not independent of each other, rather they co-exist.Most important feature of object oriented approach is defining attributes and their functionality as a single unit called class. It serves as a blueprint for all objects having similar attributes and behaviour. In OOP, class defines what are the attributes its object has, and how is its behaviour. Object, on the other hand is an instance of the class.Python is a completely object oriented language. Everything in a Python program is an object. A data item created with literal representation represents happens to be an object of respective built-in class.>>> marks=50 >>> type(marks) <class 'int'> >>> percent=55.50 >>> type(percent) <class 'float'> >>> name='Jimmy' >>> type(name) <class 'str'> >>> numbers=[12,34,43,21] >>> type(numbers) <class 'list'>You can also define objects of built-in classes using built-in functions of same name (they are actually constructors for respective classes)>>> x=int(10) >>> s=str('Hello') >>> d=dict(one=1, two=2)A module – either built-in or user-defined – when imported becomes object of module class.>>> import math >>> type(math) <class 'module'>A built-in function, function in built-in module or a user-defined function is also an object.>>> type(len) <class 'builtin_function_or_method'> >>> type(math.pow) <class 'builtin_function_or_method'> >>> def add(x,y): return x+y >>> type(add) <class 'function'>User defined ClassIn addition to built-in classes, class keyword helps in creating a customized class.  Following statement defines a class called User.>>> class User: 'docstring of User class' passAs in function or module, first string literal in the block is docstring of the class. We can declare object of this person class as we would for any built-in class.>>> a=User() >>> b=User()Objects (sometimes called instances) of User class do not have any attributes. Instance attributes are defined and initialized in Python class by a special method called constructor.ConstructorA method (similar to a function, but inside class) in the class, which is automatically invoked whenever a new object is instantiated, is called constructor. Name of constructor in Python class is __init__(). Let us provide a constructor to User class to define name, email and role as attributes.>>> class User: 'docstring of User class' def __init__(self): self.name='Ram' self.email='ram@abc.com' self.role='Manager'Each method in class must have self as first parameter. It carries reference to calling object. Attributes of calling object are accessed by self.attrib. Let us provide display() method inside User class to display attributes of calling object.def display(self): print ('name:',self.name) print ('email:',self.email) print ('role:',self.role)In following Python script, User class is defined.#user.py class User:        'docstring of User class'        def __init__(self):                self.name='Ram'                self.email='ram@abc.com'                self.role='Manager'        def display(self):                print ('name:',self.name)                print ('email:',self.email)                print ('role:',self.role)Let us use above script as a module and import User class. An object a is declared. It automatically calls __init__() constructor to initialize it attributes. Its contents are shown when display() method is called.>>> from user import User >>> a=User() >>> a.display() name: Ram email: ram@abc.com role: ManagerNote that even if more than one objects are declared, each object will be initialized with same values assigned to the attributes. You would ideally want object to be created with different specific values instead of default ones. To achieve this, __init__() method is defined with additional parameters with default values.#user.py class User:        def __init__(self, name='Ram', email='ram@abc.com', role='Manager'):                self.name=name                self.email=email                self.role=role        def display(self):                print ('name:',self.name)                print ('email:',self.email)                print ('role:',self.role)Import User class and declare two objects with different attributes. First one will have default values and second will be initialized with actual arguments provided.>>> from user import User >>> x=User() >>> x.display() name: Ram email: ram@abc.com role: Manager >>> y=User('Sunil', 'sunil@abc.com', 'admin') >>> y.display() name: Sunil email: sunil@abc.com role: adminGetters and settersTraditional object oriented languages like Java use getter and setter methods respectively to return value of and assign value to instance variables. In following Python script (book.py) defines book class having instance method getprice() which returns price attribute of the object, and setprice() which sets price attribute of object with given value.#book.py class Book:        def __init__(self, price=40):                self.price=price        def getprice(self):                return self.price        def setprice(self, price):                self.price=priceFollowing interactive session shows implementation of book class from above script.>>> from book import Book >>> b1=Book() >>> b1.getprice() 40 >>> b1.setprice(55) >>> b2=Book(100) >>> b2.getprice() 100 >>> b2.setprice(200)Data encapsulation in PythonAccording to principle of data encapsulation in Object oriented approach, instance attributes of class are available for processing to methods defined within the class only. Methods themselves on the other hand are accessible from outside class context. So object data is hidden from environment that is external to class. Method encapsulates object data so that unwarranted access to it is prevented.In Java/C++, this mechanism is implemented by use of private and public keywords. Instance attributes are encapsulated data by declaring them as private, whereas methods are given public access.Python however doesn’t have such mechanism to restrict direct access to instance variables outside the class. It is possible to manipulate price attribute of book’s object without getter/setter method also.>>> b1.price 55 >>> b1.price=25Python even has built-in functions getattr() and setattr() to fetch and set values of an instance attribute.>>> b1=Book() >>> hasattr(b1, 'price') True >>> getattr(b1,'price') 40 >>> setattr(b1, 'price',100)All instance variables and methods in Python class are public by default. To emulate behaviour of private keyword, instance variable is prefixed by double underscore (__).  Let us modify book.py to make price attribute private.#book.py class Book:        def __init__(self, price=40):                self.__price=price        def getprice(self):                return self.__price        def setprice(self, price):                self.__price=priceThis ensures that instance variable is accessible only through getter/setter method. If we try to access directly, AttributeError is raised.>>> b1=Book() >>> b1.getprice() 40 >>> b1.__price AttributeError: 'Book' object has no attribute '__price'However, the double underscore prefix doesn’t really make price truly private (as in case of Java). It merely performs name mangling. Python internally renames private variable by adding the "_ClassName" to front of variable. In this case, variable named "__price" in Book class will be mangled in “_book__price” form.>>> b1._book__price 40We can see that private instance variable is still accessible without getter/setter. This is where Python’s built-in property() function is useful.property() functionThis function provides an interface to private instance variable. It takes getter, setter and deleter methods as arguments and returns a property object corresponding to an instance attribute of a class.prop=property(fget, fset, fdel, docstring)Here fget is the getter method for a certain instance attribute, fset is its setter method and fdel is delete method. The docstring is optional explanatory string. Any type of access to this property automatically calls getter/setter method.Let us incorporate this concept and introduce price property in Book class as follows :class Book:       def __init__(self):                self.__price=40        def getprice(self):                print ("getter for price property")                return self.__price        def setprice(self,x):                print ("setter for price property")                self.__price=x        price=property(getprice, setprice, "price")The private instance variable __price is now having an interface in the form of price as property. It can be accessed directly, but it will internally invoke getprice() or setprice() method.>>> from book import Book >>> b1=Book() >>> b1.price getter for price property 40 >>> b1.price=100 setter for price propertyYou can of course use getter and setter methods as before.>>> b1.getprice() getter for price property 100 >>> b1.setprice(50) setter for price property >>> b1.price getter for price property 50Thus Python implements principle of data encapsulation in a different yet effective mechanism of defining property object.
logo

Python Tutorial

Python - Object Oriented Programming

Object oriented programming is one of the various programming paradigms in which programming languages are classified. Early programming languages developed in 50s and 60s are recognized as procedural (or procedure oriented) languages. Object oriented approach was proposed to overcome limitations of procedure oriented programming language. So, we first try to understand these limitations.

Procedural oriented approach

Every function is written in such a way that it can interface with other functions in the program. Data belonging to a function can be easily shared with other in the form of arguments, and called function can return its result back to calling function.

This approach seems to be sound one, but as the scope of program becomes larger and complex, it begins to show signs of strain, eventually leading to failure. Prominent problems related to procedural approach are as follows:

  1. The top-down approach adapted by procedure oriented languages makes the program difficult to maintain. If a change in top-level function is required, it may lead to making cascaded changes in all lower level functions.
  2. Procedural approach uses a lot of global data items, which is undesired. Especially in big and complex programs with large number of functions, too many global data items would increase memory overhead.
  3. Computer program describes procedure of data processing. However, procedural approach gives more importance to process and doesn’t consider data of same importance and takes it for granted, thereby it moves freely through the program.
  4. Movement of data across functions is unrestricted. So long as number and type of arguments match, data from any function can be sent in any other function. This is not the reflection of real life scenario where there is unambiguous association of a function with data it is expected to process.

Object oriented approach

In real world we come across, deal with and process objects, such as student, employee, invoice, car etc.. Objects are not only data and not only functions, but combination of both.

Each real world object has attributes and behaviour associated with it.Object oriented approach

Attributes: 

  1. Name, class, subjects, marks etc.. of student
  2. Name, designation, department, salary etc of employee
  3. invoice number, customer, product code and name, price and quantity etc in an invoice
  4. Registration number, owner, company, brand, horsepower, speed etc.. of car

Each attribute will have a value associated with it. Attribute is equivalent to data.

Behaviour:

  1. Processing attributes associated with an object.
  2. Compute percentage of student’s marks
  3. Calculate incentives payable to employee
  4. Apply GST to invoice value
  5. Measure speed of car

Behaviour is equivalent to function. We can see that in real life, attributes and behaviour are not independent of each other, rather they co-exist.

Most important feature of object oriented approach is defining attributes and their functionality as a single unit called class. It serves as a blueprint for all objects having similar attributes and behaviour. In OOP, class defines what are the attributes its object has, and how is its behaviour. Object, on the other hand is an instance of the class.

Python is a completely object oriented language. Everything in a Python program is an object. A data item created with literal representation represents happens to be an object of respective built-in class.

>>> marks=50
>>> type(marks)
<class 'int'>
>>> percent=55.50
>>> type(percent)
<class 'float'>
>>> name='Jimmy'
>>> type(name)
<class 'str'>
>>> numbers=[12,34,43,21]
>>> type(numbers)
<class 'list'>

You can also define objects of built-in classes using built-in functions of same name (they are actually constructors for respective classes)

>>> x=int(10)
>>> s=str('Hello')
>>> d=dict(one=1, two=2)

A module – either built-in or user-defined – when imported becomes object of module class.

>>> import math
>>> type(math)
<class 'module'>

A built-in function, function in built-in module or a user-defined function is also an object.

>>> type(len)
<class 'builtin_function_or_method'>
>>> type(math.pow)
<class 'builtin_function_or_method'>
>>> def add(x,y):
return x+y
>>> type(add)
<class 'function'>

User defined Class

In addition to built-in classes, class keyword helps in creating a customized class.  Following statement defines a class called User.

>>> class User:
'docstring of User class'
pass

As in function or module, first string literal in the block is docstring of the class. We can declare object of this person class as we would for any built-in class.

>>> a=User()
>>> b=User()

Objects (sometimes called instances) of User class do not have any attributes. Instance attributes are defined and initialized in Python class by a special method called constructor.

Constructor

A method (similar to a function, but inside class) in the class, which is automatically invoked whenever a new object is instantiated, is called constructor. Name of constructor in Python class is __init__(). Let us provide a constructor to User class to define name, email and role as attributes.

>>> class User:
'docstring of User class'
def __init__(self):
self.name='Ram'
self.email='ram@abc.com'
self.role='Manager'

Each method in class must have self as first parameter. It carries reference to calling object. Attributes of calling object are accessed by self.attrib. Let us provide display() method inside User class to display attributes of calling object.

def display(self):
print ('name:',self.name)
print ('email:',self.email)
print ('role:',self.role)

In following Python script, User class is defined.

#user.py
class User:
       'docstring of User class'
       def __init__(self):
               self.name='Ram'
               self.email='ram@abc.com'
               self.role='Manager'
       def display(self):
               print ('name:',self.name)
               print ('email:',self.email)
               print ('role:',self.role)

Let us use above script as a module and import User class. An object a is declared. It automatically calls __init__() constructor to initialize it attributes. Its contents are shown when display() method is called.

>>> from user import User
>>> a=User()
>>> a.display()
name: Ram
email: ram@abc.com
role: Manager

Note that even if more than one objects are declared, each object will be initialized with same values assigned to the attributes. You would ideally want object to be created with different specific values instead of default ones. To achieve this, __init__() method is defined with additional parameters with default values.

#user.py
class User:
       def __init__(self, name='Ram', email='ram@abc.com', role='Manager'):
               self.name=name
               self.email=email
               self.role=role
       def display(self):
               print ('name:',self.name)
               print ('email:',self.email)
               print ('role:',self.role)

Import User class and declare two objects with different attributes. First one will have default values and second will be initialized with actual arguments provided.

>>> from user import User
>>> x=User()
>>> x.display()
name: Ram
email: ram@abc.com
role: Manager
>>> y=User('Sunil', 'sunil@abc.com', 'admin')
>>> y.display()
name: Sunil
email: sunil@abc.com
role: admin

Getters and setters

Traditional object oriented languages like Java use getter and setter methods respectively to return value of and assign value to instance variables. In following Python script (book.py) defines book class having instance method getprice() which returns price attribute of the object, and setprice() which sets price attribute of object with given value.

#book.py
class Book:
       def __init__(self, price=40):
               self.price=price
       def getprice(self):
               return self.price
       def setprice(self, price):
               self.price=price

Following interactive session shows implementation of book class from above script.

>>> from book import Book
>>> b1=Book()
>>> b1.getprice()
40
>>> b1.setprice(55)
>>> b2=Book(100)
>>> b2.getprice()
100
>>> b2.setprice(200)

Data encapsulation in Python

According to principle of data encapsulation in Object oriented approach, instance attributes of class are available for processing to methods defined within the class only. Methods themselves on the other hand are accessible from outside class context. So object data is hidden from environment that is external to class. Method encapsulates object data so that unwarranted access to it is prevented.

In Java/C++, this mechanism is implemented by use of private and public keywords. Instance attributes are encapsulated data by declaring them as private, whereas methods are given public access.

Python however doesn’t have such mechanism to restrict direct access to instance variables outside the class. It is possible to manipulate price attribute of book’s object without getter/setter method also.

>>> b1.price
55
>>> b1.price=25

Python even has built-in functions getattr() and setattr() to fetch and set values of an instance attribute.

>>> b1=Book()
>>> hasattr(b1, 'price')
True
>>> getattr(b1,'price')
40
>>> setattr(b1, 'price',100)

All instance variables and methods in Python class are public by default. To emulate behaviour of private keyword, instance variable is prefixed by double underscore (__).  

Let us modify book.py to make price attribute private.

#book.py
class Book:
       def __init__(self, price=40):
               self.__price=price
       def getprice(self):
               return self.__price
       def setprice(self, price):
               self.__price=price

This ensures that instance variable is accessible only through getter/setter method. If we try to access directly, AttributeError is raised.

>>> b1=Book()
>>> b1.getprice()
40
>>> b1.__price
AttributeError: 'Book' object has no attribute '__price'

However, the double underscore prefix doesn’t really make price truly private (as in case of Java). It merely performs name mangling. Python internally renames private variable by adding the "_ClassName" to front of variable. In this case, variable named "__price" in Book class will be mangled in “_book__price” form.

>>> b1._book__price
40

We can see that private instance variable is still accessible without getter/setter. This is where Python’s built-in property() function is useful.

property() function

This function provides an interface to private instance variable. It takes getter, setter and deleter methods as arguments and returns a property object corresponding to an instance attribute of a class.

prop=property(fget, fset, fdel, docstring)

Here fget is the getter method for a certain instance attribute, fset is its setter method and fdel is delete method. The docstring is optional explanatory string. Any type of access to this property automatically calls getter/setter method.

Let us incorporate this concept and introduce price property in Book class as follows :

class Book:

       def __init__(self):
               self.__price=40
       def getprice(self):
               print ("getter for price property")
               return self.__price
       def setprice(self,x):
               print ("setter for price property")
               self.__price=x
       price=property(getprice, setprice, "price")

The private instance variable __price is now having an interface in the form of price as property. It can be accessed directly, but it will internally invoke getprice() or setprice() method.

>>> from book import Book
>>> b1=Book()
>>> b1.price
getter for price property
40
>>> b1.price=100
setter for price property

You can of course use getter and setter methods as before.

>>> b1.getprice()
getter for price property
100
>>> b1.setprice(50)
setter for price property
>>> b1.price
getter for price property
50

Thus Python implements principle of data encapsulation in a different yet effective mechanism of defining property object.

Leave a Reply

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

Comments

Eula

This is my first time here. I am truly impressed to read all this in one place.

Jaypee Dela Cruz

Thank you for your wonderful codes and website, you helped me a lot especially in this socket module. Thank you again!

lucky verma

Thank you for taking the time to share your knowledge about using python to find the path! Your insight and guidance is greatly appreciated.

Pre Engineered Metal Building

Usually I by no means touch upon blogs however your article is so convincing that I by no means prevent myself to mention it here.

Pre Engineered Metal Building

Usually, I never touch upon blogs; however, your article is so convincing that I could not prevent myself from mentioning how nice it is written.

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

C# Tutorial

C# is an object-oriented programming developed by Microsoft that uses the .Net Framework. It utilizes the Common Language Interface (CLI) that describes the executable code as well as the runtime environment. C# can be used for various applications such as web applications, distributed applications, database applications, window applications etc.For greater understanding of this tutorial, a basic knowledge of object-oriented languages such as C++, Java etc. would be beneficial.
C# Tutorial

C# is an object-oriented programming developed by Microsoft that uses ...

Read More