Object - Oriented Programming in Python
Object - Oriented Programming in Python
Programming in Python
Classes and Objects
Object-‐Oriented Programming (OOP): A programming
paradigm that involves designing programs around
concepts represented as "objects"
• Python supports OOP through the provision of classes.
• Terminology
•Class: A collection of functions and attributes,
attached to a specific name, which represents an
abstract concept.
•Attribute: A named piece of data (i.e. variable
associated with a class.
•Object: A single concrete instance generated from a
class
Instances of Classes
Classes can be viewed as factories or templates
for generating new object instances.
Each object instance takes on the properties of
the class from which it was created.
Instances of Classes
Creating Classes
Defining a class in Python is done using the class
keyword, followed by an indented block with the
class contents.
class <Classname>:
Class data
data1 = value1
attributes
...
dataM = valueM
def <function1>(self,arg1,...,argK):
Class
<block>
member
...
functions
def <functionN>(self,arg1,...,argK):
<block>
Defining Functions in Classes
• A class definition block can include multiple
functions.
• These represent the functionality or behaviors
that are associated with the class.
>>> class Maths:
... def
... subtract(sel
... f,i,j): return
... def i-j
... add(sel
f,x,y):
Argument (self) refers
returnto x+y
the object itself
Calling Functions in Classes
• Using Class Functions from Outside a Class
Functions are referenced by using the dot syntax:
<objectName>.<methodName>()
>>> >>>
p1 p1 = Person() Change
= Person()
>>> p2 = Person() Changetotoinstance attribute
instance ageage
attribute
>>> p2 = Person() affects only the associated
affects only the associated
>>> >>> p1.age = 35
p1.age = 35 instance (p2)
>>> print p2.age instance (p2)
>>> print p2.age
23
23
>>> p1 = Person()
>>> >>>
p1 p2
= Person()
= Person() Change to class attribute company
>>> >>>
p2 p1.company
= Person()= "ibm" Changealltoinstances
affects class attribute
(p1 and company
p2)
>>> >>> print p2.company
p1.company = "ibm" affects all instances (p1 and p2)
>>> 'ibm'
print p2.company
'ibm'
Constructor
• When an instance of a class is created, the class constructor
function is automatically called.
• The constructor is always named init ()
• It contains code for initializing a new instance of the class to a
specific initial state (e.g. setting instance attribute values).
>>> class Person:
Constructor function taking
... def init ( self, s ):
... self.name = s initial value for instance
... attribute name
... def hello( self ):
... print "Hello", self.name
Construct
>>> r1 = Rectangle( 10, 5 )
>>> print r1.width
object instance
10
>>> print r1.height
5
>>> print r1.area()
50
>>> print r1.color Inherited
(0, 0, 0) attribute
Overriding
When inheriting from a class, we can alter the behavior
of the original superclass by "overriding" functions (i.e.
declaring functions in the subclass with the same name).
Create Student
>>> compsci = Department()
>>> compsci.enroll( Student( "Smith", "John" ) ) instances and add
>>> compsci.enroll( Student( "Murphy", "Alice" ) ) to Department
instance
>>> for s in compsci.students:
... print "%s %s" % (s.firstname,s.lastname)
...
John Smith
Polymorphism