For anyone familiar with Object Oriented programming this gives a quick run down on the OO aspects of Python language.
A class can be created using the class
keyword.
1 2 3 4 5 |
|
object
in the class definition specifies that the class Point inherits from the inbuilt class “object”. Some other OO languages like JAVA this is implicit and doesn’t have to be mentioned during class definition. So if a class need to inherit from another class then “object” can be replaced with the required class name.
1 2 3 4 5 |
|
Initialization of class objects/instances is handled by the __init__
method which is similar to the constructor in other languages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Note that init method attributes includes “self” as the first paramater which is a reserved word. This is applicable to all the method definitions in a class. But when a class object is created the first parameter starts with the second parameter in the init method definition. The first parameter “self” is taken care by the Python interpreter.
The variable “totalSquares” which is defined outside all the methods in the class definition is a class variable and shared by all the instances. The variable “self.len” is an instance level variable and is accessible to all the methods in the class. Other variables defined in a method are local variables to the method and not accessible outside of the method. The following is an expanded example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
A class can inherit from multiple other classes. For e.g.
1 2 3 |
|
Similar to languages like Java, Python has a garbage collector (GC) to delete objects which are not referenced anymore. This prevents memory leaks by not releasing the ojects explicitly by programs as languages like C requires.
Parent class methods can be overridden by an inherited class by defining the same method header in the child class as in the parent class. As seen before the init defined in all the class definitions earlier is overriding the method with same header in “object” class which is the inbuild base class. Other base class methods which can be overridden are
def __del__(self):
destructor method which is called at the time of GC of the class object
def __repr__(self):
returns an evaluatable string represention of the class object
def __str__(self):
returns a string representation of the object like “toString” method in Java
If a method is overridden in a class but need to invoke the same method in the parent class, super can be used as in the following example. Calling super is bit different to that of other languaes.
1 2 3 4 5 6 7 8 9 10 11 |
|
Type of a class instance can be checked using the isinstance
function. For e.g. assuming that the previous code is in a file song.py
1 2 3 4 5 |
|
Operator overloading is accomplished by overriding the base class methods provided to overload operators.
def __cmp__(self,other):
comparing two class objects and returns -ve int if self < other, 0 if self == other and +ve int if self > other
def __add__(self,other):
to overload addition (+)
def __sub__(self,other):
to overload subtraction (-)
… More details are available in Python docs
This is just the tip of the iceberg. You can explore more about Python here.