1 / 18

Objects and classes

Objects and classes. Classes Act as blueprints Objects are instantiated from classes Encapsulate data : Attributes Encapsulate function : Methods Ideal for modeling real-world phenomena. Summary. Class header

ehren
Télécharger la présentation

Objects and classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objects and classes • Classes • Act as blueprints • Objects are instantiated from classes • Encapsulate data: Attributes • Encapsulate function: Methods • Ideal for modeling real-world phenomena

  2. 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

  3. Rational numbers in Python • Strange rounding-off errors: • Create class for representing rational numbers by storing numerator and denominator >>> 2/3.0 0.6666666666666663 >>> 2/6.0 0.3333333333333331 >>> 5/6.0 0.8333333333333337

  4. Rational numbers Initialization: Reduced form rational.py • 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

  5. Creating and using RationalNumber objects >>> from rational import RationalNumber >>> a = RationalNumber(3, 4) >>> b = RationalNumber(3, 6) >>> a.getValue() 0.75 >>> b.getValue() 0.5 >>> b.getNumerator() 1 >>> b.getDenominator() 2 No self argument in method calls! Python puts object reference in place of self RationalNumber b .getDenominator( )

  6. Creating and using RationalNumber objects >>> from rational import RationalNumber >>> a = RationalNumber(3, 4) >>> b = RationalNumber(3, 6) >>> a.getValue() 0.75 >>> b.getValue() 0.5 >>> b.getNumerator() 1 >>> b.getDenominator() 2 a b numerator = 3 denominator = 4 numerator = 1 denominator = 2 class RationalNumber

  7. Forgetting the self. prefix when referencing object attributes Initializes only local variables which go out of scope when constructor terminates!

  8. Comparing rationals - binary search tree a elements< a elements>= a

  9. Binary search tree a b elements< a elements>= a elements< b < a b <= elements< a

  10. Binary search tree of rational numbers

  11. Node class from course notes binary_tree.py Use this class to represent the search tree nodes: Each data attribute will be a RationalNumber

  12. Creating RationalNumber objects, adding them to a tree searchtree.py (part 1)

  13. Adding a node to a binary search tree searchtree.py (part 2) node node Recursive function! rational

  14. Testing 2/3 %:~ python –i searchtree.py Numerator: 2 Denominator: 3 Numerator: 3 Denominator: 4 Numerator: 7 Denominator: 14 Numerator: >>> root.getData().getvalue() 0.666666666666663 >>> root.getRight().getData().getvalue() 0.75 >>> root.getLeft().getData().getvalue() 0.50 1/2 3/4

  15. Searching for a node searchtree.py (part 3) Fast searching if tree is balanced

  16. Searching for a node, demonstration Search in big tree of random rational numbers searchtreetest.py

  17. Numerator: 2 Denominator? 3 Closest match: 433/650 Numerator: 4 Denominator? 5 Closest match: 43/54 Numerator: 1 Denominator? 2 Closest match: 370/737 Numerator: 4 Denominator? 9 Closest match: 391/878 Numerator: 10 Denominator? 1 Closest match: 994/103 Numerator: 5 Denominator? 3 Closest match: 521/313

  18. .. on to the exercises

More Related