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.
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:
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.
Each attribute will have a value associated with it. Attribute is equivalent to data.
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'>
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.
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' email@example.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' firstname.lastname@example.org' 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: email@example.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', firstname.lastname@example.org', 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: email@example.com role: Manager >>> y=User('Sunil', 'firstname.lastname@example.org', 'admin') >>> y.display() name: Sunil email: email@example.com role: admin
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)
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.
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 :
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.
This is my first time here. I am truly impressed to read all this in one place.
Thank you for your wonderful codes and website, you helped me a lot especially in this socket module. Thank you again!
Thank you for taking the time to share your knowledge about using python to find the path! Your insight and guidance is greatly appreciated.
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.
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.