07python_classes(1)
07python_classes(1)
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
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.
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
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)
def get_age(self):
return self.age
parentClass.__init__(self, x, y)
class student:
...
def __repr__(self):
return “I’m named ” + self.full_name
...
>>> f.__class__
< class studentClass at 010B4C6 >
Objective:
1
STRUCTURED vs. OO PROGRAMMING
STRUCTURED PROGRAMMING:
MAIN PROGRAM GLOBAL DATA
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
•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?
8
Design Principles of OOP
• Encapsulation
• Abstraction
• Polymorphism
• Inheritance
9
Encapsulation
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
Analogy:
•In English, bank can mean side of a river
or a place to put money
•move -
12
Function Overloading
13
Inheritance
Superclass
Vehicle
Subclasses
15
Object-Oriented Programming Languages
Pure OO
Languages
Smalltalk, Eiffel, Actor,
J ava
Hybrid OO
Languages
C++, Objective- C, Object-
Pascal
16