Lecture 8 - Object Oriented Programming
Lecture 8 - Object Oriented Programming
• Manipulate objects.
• Destroy objects:
• Explicitly using del or just “forget” about them.
• Destroyed/inaccessible are “garbage” and will be reclaimed through “garbage collection”.
L 1 2 3 4
Follow pointer
to next index
• Q: How to manipulate lists?
• A: L[i], L[i:j], +, len(), min(), max(), del(L[i]),
L.append(), L.extend(), L.count(), L.index(),
L.insert(), L.pop(), L.remove(), L.reverse().
• Internal Representation (IR) should be private.
• Correct behavior compromised if IR manipulated directly.
Friday, March 8, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 5
Advantages of OOP
• Bundle data into packages:
• Values + procedures to work on them through well-defined interfaces.
• Divide-and-Conquer development:
• Implement and test behavior of each class/object separately.
• Increased modularity reduces complexity.
• Similar to functions, classes make is easy to reuse code:
• Many PYTHON modules define new classes.
• Each class has a separate environment (no collision).
• Inheritance allows subclasses:
• To re-define/extend a selected subset of a superclass’ behavior.
• Creating a class:
• Defining the class’ name.
• Defining the class’ attributes.
• Other than self and dot notation, methods behave as any typical function.
Friday, March 8, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 13
Using A Method
t h od
me ition
in
def distance(self, other): def
x_diff_sq = (self.x – other.x)**2
y_diff_sq = (self.y – other.y)**2
return (x_diff_sq + y_diff_sq)**0.5
… among others …