Pages

Thursday, September 9, 2010

Object-Oriented Programming Introduction


In all our programs till now, we have designed our program around functions or blocks of statements
which manipulate data. This is called the procedure-oriented way of programming. There is another way
of organizing your program which is to combine data and functionality and wrap it inside what is called
an object. This is called the object oriented programming paradigm. Most of the time you can use procedural
programming but sometimes when you want to write large programs or have a solution that is
better suited to it, you can use object oriented programming techniques.
Classes and objects are the two main aspecs of object oriented programming. A class creates a new type
where objects are instances of the class. An analogy is that you can have variables of type int which
translates to saying that variables that store integers are variables which are instances (objects) of the
int class.
Note for C/C++/Java/C# Programmers
Note that even integers are treated as objects (of the int class). This is unlike C++ and Java
(before version 1.5) where integers are primitive native types. See help(int) for more details
on the class.
C# and Java 1.5 programmers will be familiar with this concept since it is similar to the boxing
and unboxing concept.
Objects can store data using ordinary variables that belong to the object. Variables that belong to an object
or class are called as fields. Objects can also have functionality by using functions that belong to a
class. Such functions are called methods of the class. This terminology is important because it helps us
to differentiate between functions and variables which are separate by itself and those which belong to a
class or object. Collectively, the fields and methods can be referred to as the attributes of that class.
Fields are of two types - they can belong to each instance/object of the class or they can belong to the
class itself. They are called instance variables and class variables respectively.
A class is created using the class keyword. The fields and methods of the class are listed in an indented
block.
The self
Class methods have only one specific difference from ordinary functions - they must have an extra first
name that has to be added to the beginning of the parameter list, but you do do not give a value for this
parameter when you call the method, Python will provide it. This particular variable refers to the object
itself, and by convention, it is given the name self.
Although, you can give any name for this parameter, it is strongly recommended that you use the name
self - any other name is definitely frowned upon. There are many advantages to using a standard name
- any reader of your program will immediately recognize it and even specialized IDEs (Integrated Development
Environments) can help you if you use self.
Note for C++/Java/C# Programmers
The self in Python is equivalent to the self pointer in C++ and the this reference in Java and C#.
You must be wondering how Python gives the value for self and why you don't need to give a value
for it. An example will make this clear. Say you have a class called MyClass and an instance of this
class called MyObject. When you call a method of this object as MyObject.method(arg1,
arg2), this is automatically converted by Python into MyClass.method(MyObject, arg1,
arg2 - this is what the special self is all about.
This also means that if you have a method which takes no arguments, then you still have to define the
method to have a self argument.

No comments:

Post a Comment