Object Oriented Programming
in Python:
Defining Classes
It’s all objects…
Everything in Python is really an object.
• We’ve seen hints of this already…
“hello”.upper()
list3.append(‘a’)
dict2.keys()
• These look like Java or C++ method calls.
• New object classes can easily be defined in
addition to these built-in data-types.
In fact, programming in Python is typically
done in an object oriented fashion.
Defining a Class
A class is a special data type which defines
how to build a certain kind of object.
The class also stores some data items that
are shared by all the instances of this class
Instances are objects that are created which
follow the definition given inside of the class
Python doesn’t use separate class interface
definitions as in some languages
You just define the class and then use it
Methods in Classes
Define a method in a class by including
function definitions within the scope of the
class block
There must be a special first argument self
in all of method definitions which gets bound
to the calling instance
There is usually a special method called
__init__ in most classes
We’ll talk about both later…
A simple class def: student
class student:
“““A class representing a
student ”””
def __init__(self,n,a):
self.full_name = n
self.age = a
def get_age(self):
return self.age
Creating and Deleting
Instances
Instantiating Objects
There is no “new” keyword as in Java.
Just use the class name with ( ) notation and
assign the result to a variable
__init__ serves as a constructor for the class.
Usually does some initialization work
The arguments passed to the class name are
given to its __init__() method
So, the __init__ method for student is passed
“Bob” and 21 and the new class instance is
bound to b:
b = student(“Bob”, 21)
Constructor: __init__
An __init__ method can take any number of
arguments.
Like other functions or methods, the
arguments can be defined with default values,
making them optional to the caller.
However, the first argument self in the
definition of __init__ is special…
Self
The first argument of every method is a
reference to the current instance of the class
By convention, we name this argument self
In __init__, self refers to the object
currently being created; so, in other class
methods, it refers to the instance whose
method was called
Similar to the keyword this in Java or C++
But Python uses self more often than Java
uses this
Self
Although you must specify self explicitly
when defining the method, you don’t include it
when calling the method.
Python passes it for you automatically
Defining a method: Calling a method:
(this code inside a class definition.)
def set_age(self, num): >>> x.set_age(23)
self.age = num
Deleting instances: No Need to “free”
When you are done with an object, you don’t
have to delete or free it explicitly.
Python has automatic garbage collection.
Python will automatically detect when all of the
references to a piece of memory have gone
out of scope. Automatically frees that
memory.
Generally works well, few memory leaks
There’s also no “destructor” method for
classes
Access to Attributes
and Methods
Definition of student
class student:
“““A class representing a student
”””
def __init__(self,n,a):
self.full_name = n
self.age = a
def get_age(self):
return self.age
Tr aditional Syntax for Access
>>> f = student(“Bob Smith”, 23)
>>> f.full_name # Access attribute
“Bob Smith”
>>> f.get_age() # Access a method
23
Accessing unknown members
Problem: Occasionally the name of an attribute or
method of a class is only given at run time…
Solution:
getattr(object_instance, string)
string is a string which contains the name of an
attribute or method of a class
getattr(object_instance, string)
returns a reference to that attribute or method
getattr(object_instance, string)
>>> f = student(“Bob Smith”, 23)
>>> getattr(f, “full_name”)
“Bob Smith”
>>> getattr(f, “get_age”)
<method get_age of class studentClass at
010B3C2>
>>> getattr(f, “get_age”)() # call it
23
>>> getattr(f, “get_birthday”)
# Raises AttributeError – No method!
hasattr(object_instance,string)
>>> f = student(“Bob Smith”, 23)
>>> hasattr(f, “full_name”)
True
>>> hasattr(f, “get_age”)
True
>>> hasattr(f, “get_birthday”)
False
Attributes
Two Kinds of Attributes
The non-method data stored by objects are
called attributes
Data attributes
• Va riable owned by a particular instance of a class
• Each instance has its own value for it
• These are the most common kind of attribute
Class attributes
• Owned by the class as a whole
• All class instances share the same value for it
• Called “static” variables in some languages
• Good for (1) class-wide constants and (2)
building counter of how many instances of the
class have been made
Data Attributes
Data attributes are created and initialized by
an __init__() method.
• Simply assigning to a name creates the attribute
• Inside the class, refer to data attributes using self
—for example, self.full_name
class teacher:
“A class representing teachers.”
def __init__(self,n):
self.full_name = n
def print_name(self):
print self.full_name
Class Attributes
Because all instances of a class share one copy of a
class attribute, when any instance changes it, the value
is changed for all instances
Class attributes are defined within a class definition and
outside of any method
Since there is one of these attributes per class and not
one per instance, they’re accessed via a different
notation:
• Access class attributes using self.__class__.name notation --
This is just one way to do this & the safest in general.
class sample: >>> a = sample()
x = 23 >>> a.increment()
def increment(self): >>> a.__class__.x
self.__class__.x += 1 24
Data vs. Class Attributes
class counter: >>> a = counter()
overall_total = 0 >>> b = counter()
# class attribute >>> a.increment()
def __init__(self): >>> b.increment()
self.my_total = 0 >>> b.increment()
# data attribute >>> a.my_total
def increment(self): 1
counter.overall_total = \ >>> a.__class__.overall_total
counter.overall_total + 1 3
self.my_total = \ >>> b.my_total
self.my_total + 1 2
>>> b.__class__.overall_total
3
Inheritance
Subclasses
Classes can extend the definition of
other classes
• Allows use (or extension) of methods and
attributes already defined in the previous one
To define a subclass, put the name of
the superclass in parens after the
subclass’s name on the first line of the
definition
Class Cs_student(student):
• Python has no ‘extends’ keyword like Java
• Multiple inheritance is supported
Multiple Inheritance
Python has two kinds of classes: old and new (more
on this later)
Old style classes use depth-first, left-to-right access
New classes use a more complex, dynamic approach
class AO(): x = 0 >>> from mi import *
class BO(AO): x = 1 >>> ao.x
class CO(AO): x = 2 0
class DO(BO,CO): pass >>> bo.x
ao = AO() 1
bo = BO() >>> co.x
co = CO() 2
do = DO() >>> do.x
1
>>>
http://cs.umbc.edu/courses/331/current/code/python/mi.py
Redefining Methods
To redefine a method of the parent class, include a
new definition using the same name in the subclass
• The old code won’t get executed
To execute the method in the parent class in
addition to new code for some method, explicitly
call the parent’s version of method
parentClass.methodName(self,a,b,c)
The only time you ever explicitly pass ‘self’ as an
argument is when calling a method of an ancestor
Definition of a class extending student
Class Student:
“A class representing a student.”
def __init__(self,n,a):
self.full_name = n
self.age = a
def get_age(self):
return self.age
Class Cs_student (student):
“A class extending student.”
def __init__(self,n,a,s):
student.__init__(self,n,a) #Call __init__ for student
self.section_num = s
def get_age(): #Redefines get_age method entirely
print “Age: ” + str(self.age)
Extending __init__
Same as redefining any other method…
• Commonly, the ancestor’s __init__ method is
executed in addition to new commands
• Yo u’ll often see something like this in the
__init__ method of subclasses:
parentClass.__init__(self, x, y)
where parentClass is the name of the parent’s class
Special Built-In
Methods and Attributes
Built-In Members of Classes
Classes contain many methods and
attributes that are always included
• Most define automatic functionality triggered
by special operators or usage of that class
• Built-in attributes define information that
must be stored for all classes.
All built-in members have double
underscores around their names:
__init__ __doc__
Special Methods
E.g., the method __repr__ exists for all
classes, and you can always redefine it
__repr__ specifies how to turn an instance
of the class into a string
•print f sometimes calls f.__repr__() to
produce a string for object f
• Ty ping f at the REPL prompt calls
__repr__ to determine what to display as
output
Special Methods – Example
class student:
...
def __repr__(self):
return “I’m named ” + self.full_name
...
>>> f = student(“Bob Smith”, 23)
>>> print f
I’m named Bob Smith
>>> f
“I’m named Bob Smith”
Special Methods
You can redefine these as well:
__init__ : The constructor for the class
__cmp__ : Define how == works for class
__len__ : Define how len( obj ) works
__copy__ : Define how to copy a class
Other built-in methods allow you to give a class
the ability to use [ ] notation like an array or ( )
notation like a function call
Special Data Items
These attributes exist for all classes.
__doc__ : Va riable for documentation string for
class
__class__ : Va riable which gives you a reference to
the class from any instance of it
__module__ : Va riable which gives a reference to the
module in which the particular class is defined
__dict__ :The dictionary that is actually the
namespace for a class (but not its superclasses)
Useful:
• dir(x)
returns a list of all methods and attributes
defined for object x
Special Data Items – Example
>>> f = student(“Bob Smith”, 23)
>>> print f.__doc__
A class representing a student.
>>> f.__class__
< class studentClass at 010B4C6 >
>>> g = f.__class__(“Tom Jones”,
34)
Private Data and Methods
Any attribute/method with two leading under-
scores in its name (but none at the end) is
private and can’t be accessed outside of
class
Note: Names with two underscores at the
beginning and the end are for built-in
methods or attributes for the class
Note: There is no ‘protected’ status in Python;
so, subclasses would be unable to access
these private data either
INTRODUCTION TO OOP
Objective:
•Know the difference between functional programm
and OOP
• Know basic terminology in OOP
• Know the importance of OOP
• Know four design principles of OOP
• Know OOP programming languages
1
STRUCTURED vs. OO PROGRAMMING
STRUCTURED PROGRAMMING:
MAIN PROGRAM GLOBAL DATA
FUNCTION FUNCTION 2 FUNCTION 3
1
FUNCTION 4 FUNCTION 5
2
Structured Programming
Using function
Function & program is divided into modules
Every module has its own data and function
which can be called by other modules.
3
OBJECT ORIENTED PROGRAMMING
Object 2
Object 1
Data Data
Function Function
Object 3
Data
Function
4
OBJECT ORIENTED PROGRAMMING
•Objects have both data and methods
• Objects of the same class have the same data
elements and methods
• Objects send and receive messages to invoke
actions
Key idea in object-
oriented:
The real world can be accurately described as a
collection of
objects that interact.
5
Basic terminology
•object
- usually a person, place or thing (a noun)
•method
- an action performed by an object (a verb)
•attribute
- description of objects in a class
•class
- a category of similar objects (such as
automobiles)
- does not hold any values of the object’s 6
Example for attributes and methods
Attributes: Methods:
manufacturer’s Define data items
name (specify
model name manufacturer’s name,
model, year, etc.)
year made Change a data item
color (color, engine, etc.)
number of doors Display data items
size of engine Calculate cost
etc. etc.
7
Why OOP?
• Save development time (and cost) by
reusing code
–once an object class is created it can be
used in other applications
• Easier debugging
–classes can be tested independently
–reused objects have already been tested
8
Design Principles of OOP
Four main design principles of Object-
Oriented Programming(OOP):
• Encapsulation
• Abstraction
• Polymorphism
• Inheritance
9
Encapsulation
•Also known as data hiding
•Only object’s methods can modify
information in the object.
Analogy:
• AT M machine can only update accounts of
one person or object only.
10
Abstraction
Focus only on the important facts about the
problem at hand
to design, produce, and describe so that it can
be easily used without knowing the details of
how it works.
Analogy:
When you drive a car, you don’t have to know
how the gasoline and air are mixed and ignited.
Instead you only have to know how to use the
controls.
Draw map
11
Polymorphism
•the same word or phrase can mean
different things in different contexts
Analogy:
•In English, bank can mean side of a river
or a place to put money
•move -
12
Function Overloading
The operation of one function
depends on
the argument passed to it.
Example: Fly(), Fly(low), Fly(150)
13
Inheritance
• Inheritance—a way of organizing classes
• Term comes from inheritance of traits like
eye color, hair color, and so on.
• Classes with properties in common can be
grouped so that their common properties are
only defined once.
• Superclass – inherit its attributes &
methods to the subclass(es).
• Subclass – can inherit all its superclass
attributes & methods besides having its own
unique attributes & methods. 14
An Inheritance Hierarchy
Superclass
Vehicle
Subclasses
Automobile Motorcycle Bus
Sedan Sports Car Luxury Bus School Bus
What properties does each vehicle inherit from the types
of vehicles above it in the diagram?
15
Object-Oriented Programming Languages
Pure OO
Languages
Smalltalk, Eiffel, Actor,
J ava
Hybrid OO
Languages
C++, Objective- C, Object-
Pascal
16