1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.
-
Upload
violet-andrews -
Category
Documents
-
view
226 -
download
0
Transcript of 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.
![Page 1: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/1.jpg)
1
Programming for Engineers in
Python
Autumn 2011-12
Lecture 6: More Object Oriented Programming
![Page 2: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/2.jpg)
2
Lecture 5 Highlights
• Functions review
• Object Oriented Programming
![Page 3: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/3.jpg)
3
is and== is will return True if two variables point to the same object, == if the objects referred to by the variables are equal
>>> a = [1, 2, 3]>>> b = a>>> b is a True>>> b == aTrue>>> b = a[:]>>> b is aFalse>>> b == aTrue
![Page 4: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/4.jpg)
4
Object-Oriented Programming (OOP)
• Represent problem-domain entities using a computer language
• When building a software in a specific domain, describe the different components of the domain as types and variables
• Thus we can take another step up in abstraction
![Page 5: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/5.jpg)
5
Class as a BlueprintA class is a blueprint of objects
![Page 6: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/6.jpg)
6
Car Example
• Members: 4 wheels, steering wheel, horn, color,…
• Every car instance has its own
• Methods: drive, turn left, honk, repaint,…
• Constructors: by color (only), by 4 wheels, engine,…
![Page 7: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/7.jpg)
7
Shapes – 2D Point, Circle• __init__• self• Attributes• Instances and memory• Copy (shallow / deep)• Methods
![Page 8: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/8.jpg)
8
Code – Define Classes
![Page 9: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/9.jpg)
9
Code – Using Classes
![Page 10: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/10.jpg)
10
Today• Continue with 2D Shapes• Rational numbers implementation
• It should feel like native language support
• Inspired by chapter 6 from the book Programming in Scala
![Page 11: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/11.jpg)
11
A Rectangle (design options)
• It is not always obvious what the attributes of an object should be
• How would you represent a rectangle?• (for simplicity ignore angle, assume the rectangle is
vertical or horizontal)• There are several possibilities:
• One corner / center point + width and height• Two opposing corners
• We shall select the width, height, lower-left corner
![Page 12: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/12.jpg)
12
A Rectangle - ImplementationIn class Rectangle:
Shell:
![Page 13: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/13.jpg)
13
Rectangle in Memory
![Page 14: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/14.jpg)
14
Find Center
In class Rectangle:
Shell:
![Page 15: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/15.jpg)
15
Grow Rectangle
In class Rectangle:
Shell:
![Page 16: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/16.jpg)
16
Has Attributes?
![Page 17: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/17.jpg)
17
Print All Attributes
![Page 18: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/18.jpg)
18
Inheritance (briefly)
• The general idea• class Point(object) – what does object stands for?• Example: Animals• Polymorphism
• We have seen that already!• Histogram example
![Page 19: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/19.jpg)
19
Histogram (polymorphism)
Source: Think Python
![Page 20: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/20.jpg)
20
Rational Numbers
• A rational number is a number that can be expressed as a ratio n/d (n, d integers, d not 0)
• Examples: 1/2, 2/3, 112/239, 2/1
• Not an approximation!
![Page 21: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/21.jpg)
21
Specification• print should work smoothly
• Add, subtract, multiply, divide
• Immutable
• It should feel like native language support
![Page 22: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/22.jpg)
22
Constructing a Rational• What are the attributes?• How a client programmer will create a new Rational
object?
![Page 23: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/23.jpg)
23
Constructing a Rational
?
Shell:
![Page 24: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/24.jpg)
24
Reimplementing __str__• __str__ method return a string representation of an object• A more useful implementation of __str__ would print out
the values of the Rational’s numerator and denominator• override the default implementation
In class Rational:
Shell:
![Page 25: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/25.jpg)
25
__repr__• __repr__ method returns the “official” string
representation of an object
In class Rational:
Shell:
![Page 26: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/26.jpg)
26
Checking Preconditions
• Ensure the data is valid when the object is constructed
In class Rational:
![Page 27: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/27.jpg)
27
Checking Preconditions
![Page 28: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/28.jpg)
28
Defining Operators• Why not use natural arithmetic operators?
• Operator precedence will be kept
• All operations are method calls
From the book Programming in Scala
![Page 29: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/29.jpg)
29
Operator Overloading
• By defining other special methods, you can specify the behavior of operators on user defined types
• +, -, *, /, <, >,…
![Page 30: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/30.jpg)
30
Adding Rational Numbers
![Page 31: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/31.jpg)
31
Define __add__ Method
• Immutable
In class Rational:
Shell:
![Page 32: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/32.jpg)
32
Other Arithmetic Operations
In class Rational:
![Page 33: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/33.jpg)
33
Other Arithmetic Operations
![Page 34: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/34.jpg)
34
<, >, max
INCORRECT!
![Page 35: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/35.jpg)
35
<, >, max
In class Rational:
![Page 36: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/36.jpg)
36
<, >, max
How come max works?
![Page 37: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/37.jpg)
• Constructors other then the primary? • Example: a rational number with a denominator
of 1 (e.g., 5/1 5)• We would like to do: Rational(5)
• Default arguments• Useful not solely for constructors
• Remember sorted (reverse, key are default arguments)?37
Default Arguments to Constructor
![Page 38: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/38.jpg)
38
Revised Rational
In class Rational:
Shell:
![Page 39: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/39.jpg)
39
Greatest Common Divisor (gcd)
• 66/42 = 11/7• To normalize divide the numerator and
denominator by their greatest common divisor (gcd)• gcd(66,42) = 6 (66/6)/(42/6) = 11/7• No need for Rational clients to be aware of this• Encapsulation
![Page 40: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/40.jpg)
40
Off Topic: Calculate gcd(Only if time allows)
• gcd(a,b) = g• a = n * g• b = m * g• gcd(n,m)=1(otherwise g is not the gcd)• a = t * b + r = t * m * g + r g is a divisor of r
• gcd(a,b) = gcd(b,a%b)• The Euclidean algorithm: repeat iteratively:
if (b == 0) return aelse repeat using a b, b a%b
• http://en.wikipedia.org/wiki/Euclidean_algorithm
![Page 41: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/41.jpg)
41
Correctness• Example:
gcd(40,24) gcd(24,16) gcd(16,8) gcd(8,0) 8
• Prove: g = gcd(a,b) = gcd(b,a%b)= g1• g1 is a divisor of a ( g1 ≤ g)• There is no larger divisor of a ( g1 ≥ g)
• ≤ : a = t * b + r a = t * h * g1 + v * g1 g1 is a divisor of a
• ≥ : assume g > g1 a = t * b + r g is a divisor of b and r contradiction
![Page 42: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/42.jpg)
42
gcd Implementation
• Let’s leave it for next lesson (Recursion)• Actually, we can use the implementation in the
module fractions.gcd
![Page 43: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/43.jpg)
43
Revised Rational
In class Rational:
Shell:
![Page 44: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/44.jpg)
44
Mixed Arithmetic's• Now we can add and multiply rational numbers!• What about mixed arithmetic?
• r + 2 won’t work
• r + Rational(2) is not nice
• Add new methods for mixed addition and multiplication
• Will work thanks to polymorphism
![Page 45: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/45.jpg)
45
Usage
• The + method invoked is determined in each case by the type of the right operand
• In our code it is implemented only for the operator + on integers (in “real” life it should have been implemented for every operator and every data type that is supported)
![Page 46: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/46.jpg)
46
Revised __add__• Isinstance takes a value and a class object, and returns
True if the vlaue is an instance of the class• Handles addition of integers correctly• Type based dispatch – dispatches the computation to
different executions based on the types of the arguments
![Page 47: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/47.jpg)
47
Implicit Conversions
• 2 + r 2.+(r) method call on 2 (int) int class contains no __add__ method that takes a Rational argument
• The problem: Python is asking an integer to add a Rational object, and it doesn’t know how to do that
![Page 48: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/48.jpg)
48
__radd__ - Right Side Add
In class Rational:
Shell:
• __radd__ invoked when a Rational object appears on the right side of the + operator
![Page 49: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/49.jpg)
49
Summary• Customize classes so that they are natural
to use• Attributes, methods, constructor• Method overriding• Encapsulation• Define operators as method• Method overloading
![Page 50: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/50.jpg)
50
Rational Numbers in Python
• Actually, there is a Python implementation of Rational numbers
• It is called fractions http://docs.python.org/library/fractions.html
![Page 51: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 6: More Object Oriented Programming.](https://reader035.fdocuments.us/reader035/viewer/2022062423/56649f515503460f94c74ae4/html5/thumbnails/51.jpg)
51
Next Week
• No class (tirgulim as usual)• Next topic: Recursion