Objects and classes Classes Act as blueprints Objects
Objects and classes • Classes – Act as blueprints • Objects are instantiated from classes – Encapsulate data: Attributes – Encapsulate function: Methods – Ideal for modeling real-world phenomena 1
Summary – Class header • Keyword class begins definition, followed by name of class and colon (: ) – Body of class • Indented block of code – Optional documentation string • Describes the class • Appears immediately after class header – Constructor method __init__ • Executes each time an object is created • Initialize attributes of class • Returns None – Object reference • All methods must at least specify this one parameter (self) • Represents object on which a method is called 2
Rational numbers in Python • Strange rounding-off errors: >>> 2/3. 0 0. 666666663 >>> 2/6. 0 0. 333333331 >>> 5/6. 0 0. 833333337 • Create class for representing rational numbers by storing numerator and denominator 17
Rational numbers rational. py Initialization: Reduced form • Use float to make sure result of division will be a float, not a rounded down integer • Object reference self first argument in all methods • self refers to the object on which the method is called 18
Creating and using Rational. Number objects >>> from rational import Rational. Number >>> a = Rational. Number(3, 4) >>> b = Rational. Number(3, 6) >>> a. get. Value() 0. 75 >>> b. get. Value() 0. 5 >>> b. get. Numerator() 1 >>> b. get. Denominator() 2 No self argument in method calls! Python puts object reference in place of self Rational. Number b. get. Denominator( ) 19
Creating and using Rational. Number objects a >>> from rational import Rational. Number >>> a = Rational. Number(3, 4) >>> b = Rational. Number(3, 6) >>> a. get. Value() 0. 75 b >>> b. get. Value() 0. 5 numerator = 3 numerator = >>> b. get. Numerator() denominator = 4 denominator 1 >>> b. get. Denominator() 2 1 = 2 class Rational. Number 20
Forgetting the self. prefix when referencing object attributes Initializes only local variables which go out of scope when constructor terminates! 21
Comparing rationals - binary search tree a elements < a elements >= a 22
Binary search tree a b elements < a elements < b < a elements >= a b <= elements < a 23
Binary search tree of rational numbers 24
binary_tree. py Node class from course notes Use this class to represent the search tree nodes: Each data attribute will be a Rational. Number 25
searchtree. py (part 1) Creating Rational. Number objects, adding them to a tree 26
searchtree. py (part 2) Adding a node to a binary search tree node Recursive function! rational 27
Testing %: ~ python –i searchtree. py Numerator: 2 Denominator: 3 1/2 Numerator: 3 Denominator: 4 Numerator: 7 Denominator: 14 Numerator: >>> root. get. Data(). getvalue() 0. 66666663 >>> root. get. Right(). get. Data(). getvalue() 0. 75 >>> root. get. Left(). get. Data(). getvalue() 0. 50 2/3 3/4 28
searchtree. py (part 3) Searching for a node Fast searching if tree is balanced 29
Searching for a node, demonstration searchtreetest. py Search in big tree of random rational numbers 30
Numerator: 2 Denominator? 3 Closest match: Numerator: 4 Denominator? 5 Closest match: Numerator: 1 Denominator? 2 Closest match: Numerator: 4 Denominator? 9 Closest match: Numerator: 10 Denominator? 1 Closest match: Numerator: 5 Denominator? 3 Closest match: 433/650 43/54 370/737 391/878 994/103 521/313 31
. . on to the exercises 32
- Slides: 18