OOP: Preliminaries

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 5 minutes

Object Oriented Python

Object Oriented Python

Hi, and welcome to Object Oriented Python. In this chapter, we'll cover the preliminary steps necessary for the rest of this course.

Table of Contents


Object Oriented Programming in Python: Introduction

Hi. Welcome to Object Oriented Programming in Python. This course will take you through the basics of the object oriented paradigm in Python. Object Oriented Programming is one of the most widely used approaches to programming. It is built around two key concepts: classes and instances. A class defines a template or a blueprint for a type of entity in the given scenario. This blueprint is in the form of attributes and associated functionalities. A class spawns one or more objects, called instances. These instances store data in the defined attributes, and call associated functionality in the form of special functions called methods. We will study how this style of programming is built on principles of Inheritance, Encapsulation and Polymorphism. These terms may not make much sense to you right now, especially if you are new to Object Oriented Programming, but they are critical to this style of programming.

If you are already familiar with the basics of Python, this is the next logical step in knowing more about the language as well as in enhancing your knowledge of programming. The concepts you will learn throughout this course are applicable in other languages as well, such as C++, PHP and Java.

Before we begin, let me tell you more about myself, the object oriented paradigm and the comparison between the procedural paradigm and the object oriented paradigm.


About the instructor

Skip this and jump to Object Oriented Programming (OOP)

Lakshay Arora @ djangoSpin

I am Lakshay Arora, and I work as a System Analyst for Amdocs. When I am on my own, I spend my time exploring various technologies. Over the past 5 years, ever since I started college, I have worked with HTML, CSS, javascript, AJAX, Perl, Java, Servlets and JSPs, Flash, Unix, SQL, Python among other things.

I had huge help from Mr John W Purcell, from Hungary, who runs the famous website Cave of Programming. I should state that while I am not proficient in most of these languages, I do have the required working knowledge. You can view my work at www.lakshayarora.com. I hosted many of my fundamental projects here, before I started this blog with a friend of mine, at djangoSpin, to share all that we know about anything and everything. I have been interested in Python mainly because of its emphasis on readability and succinctness.

I didn’t really like the machine-level work involved with C/C++. Things like pointers, memory management, and other concepts were difficult for me to comprehend, much less effectively apply. Java, as my first programming class in college, was a start but its object-oriented programming (OOP) concepts gave me fits. While I can work with many languages, I never thought to myself that I am proficient in anything other than HTML, but I wanted to devote my career to one major language. I have always been in awe of developers who spend so many years to develop a programming language, so that the general folk can use it for the good of mankind. Hence, my desire to excel in at least one of them, so that I can apply it in real-life situations, to save effort and maximize productivity.

Then I learnt that Google employs Python extensively, and I was fascinated how it could power the most powerful search engine of the world. I researched about it a bit and found that it has a massive footprint all over the internet, and how versatile it is. Then I thought to myself, maybe this is the language for me. As I studied about it, I realized that it is incredibly easy to grasp because of its emphasis on readability, and thought I would share my knowledge with the rest of the world.

Currently, I am working in Django, which is a web framework in Python. Django is an open-source framework that takes the complexity out of web development and at the same time, it gives you control over as much as you want.

If you have any questions for me, please send me a mail at la@lakshayoarora.com.


Object Oriented Programming (OOP)

Object Oriented Programming is an approach centring around classes and objects. This is in contrast to another popular approach called the Procedural Paradigm, wherein the program is written as a logical procedure which processes input and returns output. OOP, on the other hand, employs objects to manipulate data. These objects provide a neat way to tackle complex problems, and allows for easy maintenance of code.

Solutions using this approach begin with identifying the entities which will be operated on. Then, respective blueprints of these entities are defined, with the data each entity will contain, using attributes and any functionality it will possess, using methods. These blueprints, called classes, can be inherited by other classes, using what is called Inheritance. This makes for better organization of code and better design of solution than in the Procedural Paradigm. Using this feature, the user can even enhance the functionality of builtin data types. For example, you can make the indexing of Python lists start from 1 rather than the default 0. We will discuss just how to do this later.

Object Oriented Programming first came to the scene in 1960s, when it was employed in a language called Simula67. The next language to use it extensively was Smalltalk, and among modern languages, Python, Java, C++, Ruby and Basic .NET are popular ones. It is regarded as one of the most significant stepping stones in Software Design. Three foundation principles of OOP are Encapsulation, Inheritance & Polymorphism. As I said earlier, these may not mean much to you right now, we will discuss these in detail in the upcoming chapters.


Comparison between POP and OOP

Procedure Oriented Programming, or POP, is built around procedures/functions/subroutines. The main entities in POP are functions, which contain a series of statements and perform a particular task. These functions get input from user, operate on the data, and produce output. A popular language supporting only POP is C.

Object Oriented Programming, or OOP, is built around objects and classes. A class defines a template or a blueprint for a type of entity in the given scenario. This blueprint is in the form of attributes and associated functionalities. A class spawns one or more objects, called instances. These instances store data in the defined attributes, and call associated functionality in the form of special functions called methods.

Python supports both of these paradigms. Let's look at corresponding code solutions for a problem in POP & OOP.

# POP
>>> def greet(name):
	return "Hello from {}!".format(name)

>>> person = 'Ethan'
>>> greet(person)							# 'Hello from Ethan!'

# OOP
>>> class Person(object):
	def __init__(self, name):
		self.name = name
	def greet(self):
		return "Hello from {}!".format(self.name)

	
>>> person = Person('Ethan')
>>> person.greet()							# 'Hello from Ethan!'

Do not intimidated by the code here, it does seem complex from the outset. It does not take long to get familiar with the jargon and the technicalities involved.


Object Oriented Python

Everything in Python is an object. The built-in data types such as str, int etc. are all objects. Every object is an instance of a particular class, holds some data as defined in the class template and has associated functionality in the form of methods defined in the class template. The builtin type() function tells us the class to which an object belongs to. The words object and instance are used interchangeably.

>>> type(4)
<class 'int'>

>>> type('string')
<class 'str'>

>>> type( [1, 2, 4] )
<class 'list'>

>>> type(False)
<class 'bool'>

>>> type(None)
<class 'NoneType'>

>>> type(type(4))
<class 'type'>

>>> def myFunc(): pass
>>> type(myFunc)
<class 'function'>

Note how the class of type 'class' is also defined. This highlights the fact that consistency matters to the developers of Python. It is a conscious effort on part of the development team of Python, to ensure that everything in it is an object of some class. This is in contrast to other programming languages, wherein this is not true. This feature of Python makes it easier to use as well as to extend it.


Prerequisites

In order to make the most of this course on Object Oriented Programming in Python, it is advised that you are familiar with the basics of Python. If you are not, here's a link to Python 101.


On the Agenda in Next Chapter

In the next chapter, we will begin with classes and objects. We will try to develop a basic understanding of how classes work in Python. Till then!

 


 

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply