Download Python OOP: Classes, Encapsulation, Inheritance, Polymorphism & Special Methods - Prof. De and more Lab Reports Computer Systems Networking and Telecommunications in PDF only on Docsity!
CSCI 553: Networking III
Unix Network Programming
Spring 2007
Object Oriented Programming
Today's Agenda
ļ® A Word about the Test
ļ® Books, Notes & Account
ļ® Assigned Seats
ļ® Study Questions
ļ® Objects & Testing
ļ® Set up .bashrc for JUnit usage
ļ® Lab 05
ļ® Finish 5.1 before leaving
Objects to the Rescue
ļ® Object-oriented programming (OOP) solves both problems
ļ® Each object knows how to update itself
ļ® Don't have to change old code when adding new types of things
ļ® No chance of calling the wrong function
ļ® Seems like a small change, but it allows programmers to think and design at a
higher level
ļ® Also allows them to make more powerful mistakesā¦
ļ® Ideas apply to all modern languages
ļ® But there's more variation in form than there is with loops, conditionals, and
functions
for time in simulation_period: for thing in world: thing.update(time)
Abstract Data Types
ļ® Modern languages encourage programmers to
define abstract data types (ADTs)
ļ® āAbstractā because they hide the details of their
implementation
ļ® Programmers interact with them through a limited
set of operations, rather than by manipulating data
directly
ļ® Fewer things can go wrong
ļ® Easier to read resulting code
ļ® Makes code easier to maintain, since internals can be
changed without changing calling code
Classes and Instances
Figure 1: Memory Model for Classes and Objects
Defining a Class
ļ® Define a class in Python using the class keyword
ļ® Name of the new class is usually followed by object in parentheses
ļ® We'll see other options later
ļ® Then ":" and an indented block containing the class's contents
class Empty(object):
pass
ļ® pass means ādo nothingā, i.e., create an empty class
ļ® Not particularly useful, but you have to start somewhere
Methods
ļ® Give the class methods by defining functions inside it
ļ® The object itself is always passed to the method as its first argument
ļ® Universally called self
ļ® Unlike this in C++ and Java, the name is just a convention
ļ® But everyone uses it, and you should too
ļ® object.method(argument) is equivalent to:
ļ® Find the class C that object is an instance of
ļ® Call C.method(object, argument)
class Greeting(object): def say(self, name): print 'Hello, %s!' % name
if name == 'main': greet = Greeting() greet.say('object')
Hello, object!
Creating Members
ļ® Every object is a new scope for variable
names
ļ® Just like a module, or a function call
ļ® The values in an object's scope are its
members
ļ® Create members use dotted notation: self.x = 3
ļ® Gives the current object a new member x with the
value 3
ļ® Or overwrites the existing member x with the value 3
Creating Members
Figure 2: Creating a Simple Point
Encapsulation
ļ® Encapsulation is one of the three defining principles of OOP
ļ® (^) Programs are much easier to write, read, and maintain if object members are only ever accessed by methods
ļ® But unlike C++, Java, and C#, Python doesn't allow programmers to hide methods
or data members
p = Point() p.x = 3. p.y = 4. print 'point is', p.get_values() point is (3.5, 4.25)
ļ® Any function or method can see and modify any object's internals
ļ® (^) Resist the temptation to program this way! ļ® (^) If you manipulate an object's internals directly, you have to change your program when you change the object's implementation
Constructors
class Point(object): def init(self, x=0, y=0): self.reset(x, y) def reset(self, x, y): assert (type(x) is int) and (x >= 0), 'x is not non-negative integer' assert (type(y) is int) and (y >= 0), 'y is not non-negative integer' self.x = x self.y = y def get(self): return (self.x, self.y) def norm(self): return math.sqrt(self.x ** 2 + self.y ** 2)
if name == 'main': p = Point(1, 1) print 'point is initially', p.get() p.reset(2, 2) print 'p moved to', p.get() point is initially (1, 1) p moved to (2, 2)
Constructor Style
ļ® A class can only have one constructor
ļ® Some languages allow classes to have several, distinguished by
argument types
ļ® But since Python doesn't use type declarations, this wouldn't work
ļ® It's good style to create all of the object's members in the
constructor
ļ® So that people only have to look in one place to find what members
exist
ļ® Note how the class checks values before changing the
object's state
ļ® Remember: fail early, fail often
New Classes from Old
ļ® Suppose we have a class Organism that represents living things
ļ® Common name, scientific name, ā¦
ļ® Want to create a class Mammal
ļ® Body temperature, gestation period, ā¦
ļ® Wrong: copy Organism's definition and add more members and
methods
ļ® āAnything repeated in two or more places will eventually be wrong in at
least one.ā
ļ® Right: use inheritance
ļ® The second defining principle of OOP
ļ® Derive a child class from a parent
ļ® The child has all the members and methods of its parents, plus whatever
else we give it
Inheritance Example
class Organism(object): def init(self, common_name, sci_name): self.common_name = common_name self.sci_name = sci_name def get_common_name(self): return self.common_name def get_sci_name(self): return self.sci_name def str(self): return '%s (%s)' % (self.common_name, self.sci_name)
class Mammal(Organism): def init(self, common_name, sci_name, body_temp, gest_period): Organism.init(self, common_name, sci_name) self.body_temp = body_temp self.gest_period = gest_period def get_body_temp(self): return self.body_temp def get_gest_period(self): return self.gest_period def str(self): extra = ' %4.2f degrees / %d days' % (self.body_temp, self.gest_period) return Organism.str(self) + extra
if name == 'main': creature = Mammal('wolf', 'canis lupus', 38.7, 63) print creature
wolf (canis lupus) 38.70 degrees / 63 days