Object Oriented Python
By: Dr. Vipin
Kumar
OOP Terminology
Class: A user-defined prototype for an object that defines a set of
attributes that characterize any object of the class. The attributes are
data members (class variables and instance variables) and methods,
accessed via dot notation.
Class variable: A variable that is shared by all instances of a class.
Class variables are defined within a class but outside any of the
class's methods.
Data member: A class variable or instance variable that holds data
associated with a class and its objects.
Function overloading: The assignment of more than one behavior
to a particular function. The operation performed varies by the types
of objects or arguments involved.
Instance variable: A variable that is defined inside a method and
belongs only to the current instance of a class.
OOP Terminology
conti
Inheritance: The transfer of the characteristics of a class to other
classes that are derived from it.
Instance: An individual object of a certain class.
Instantiation: The creation of an instance of a class.
Method : A special kind of function that is defined in a class
definition.
Object: A unique instance of a data structure that's defined by its
class. An object comprises both data members (class variables and
instance variables) and methods.
Operator overloading: The assignment of more than one
function to a particular operator.
Example-01
Special method,
which is called class
constructor or
initialization method.
It create a new
instance of this
class.
Example-02
Class Inheritance
Class
Base1
Class
Base2
Class Base4
Class
Base5
Class
Base3
Class Inheritance: Example-01
Class Inheritance: Example-02
Calling the
constructor
of the
parent
class
Multiple Class Inheritance: Example
Class Attributes
In Python, Built-In
built-in attributes
can be accessed
using dot operator
__dict__:
namespace.
Dictionary containing the class's
__doc__:
undefined.
Class documentation string or none, if
__name__:
Class name.
__module__: Module name in which the class is defined.
This attribute
is "__main__" in interactive mode.
__base__:
classes,
A possibly empty tuple containing the base
Built-In Class Attributes: Example
Built-In Class Attributes: Example Output
Functions : issubclass() and isinstance()
issubclass() and isinstance()
functions are used to check a
relationships of two classes and
instances respectively.
issubclass(sub,
sup)
isinstance(obj,
Class)
Boolean function returns
true if the given subclass
sub is indeed a subclass
of the superclass sup.
Boolean function returns true
if obj is an instance of class
Class or is an instance of a
Garbage Collection (Destroying Objects)
Base Overloading Methods
Lists of some generic functionality that
you can override in your own classes
Method Name Description
__init__(self,
Constructor
*args)
__del__(self)
Destructor
__str__(self)
Printable string
representation
__cmp__(self, x)
Object
comparision
__repr__(self)
Evaluating
Sample call
obj=ClassNa
me(args)
del obj
str(obj)
cmp(obj,x)
repr(obj)
Example-02 (Overriding Method)
Whenever, the name of a
method is same in parent class
and child class then, method
calling from the child method
only call the same class method.
Data Hiding
An object's attributes may or may not be visible
outside the class definition.
You need to name attributes with a double underscore
prefix.
Double underscore
for data hiding
Using class name with
underscore followed by
hided data can be
access as follow
Operators Overloading
Suppose you have created a Vector
class to represent two-imensional
vectors, what happens when you use
the plus operator to add them? Most
likely Python will yell at you.
You could, however, define the
__add__ method in your class to
perform vector addition and then the
plus operator would behave as per
expectation
Operators Overloading: Example