Python Programming - VII. Customizing Classes and Operator Overloading

1,336 views

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,336
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
193
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Python Programming - VII. Customizing Classes and Operator Overloading

  1. 1. SEQUENCE OVERLOADING However, too much overloading may also backfire.  VII. CUSTOMIZING CLASSES PYTHON PROGRAMMING Engr. Ranel O. Padon
  2. 2. PYTHON PROGRAMMING TOPICS I •Introduction to Python Programming II •Python Basics III •Controlling the Program Flow IV •Program Components: Functions, Classes, Packages, and Modules V •Sequences (List and Tuples), and Dictionaries VI •Object-Based Programming: Classes and Objects VII •Customizing Classes and Operator Overloading VIII •Object-Oriented Programming: Inheritance and Polymorphism IX •Randomization Algorithms X •Exception Handling and Assertions XI •String Manipulation and Regular Expressions XII •File Handling and Processing XIII •GUI Programming Using Tkinter 2
  3. 3. INTRODUCTION Customization provides solutions for various needs.
  4. 4. INTRODUCTION Customization has pros and cons.
  5. 5. INTRODUCTION Special Methods: __init__ and __del__ __init__ creates objects __del__ destroys objects 5
  6. 6. INTRODUCTION The typical method-call notation is cumbersome for mathematical classes. >> polynomial1.add(polynomial2) better or more natural way: >> polynomial1 + polynomial2 This is called Operator Overloading. 6
  7. 7. INTRODUCTION For faster development, reuse, modify, or extend the built-in attributes, methods & operators of a Class. 7
  8. 8. INTRODUCTION Python enables the programmer to overload most operators to be sensitive to the context in which they are used. >> print 2 + 3 >> print “Mang” + “Jose” >> print 2 * 3 >> print “Mang” * 2 #prints “MangMang” 8
  9. 9. INTRODUCTION The interpreter takes the appropriate action based on the manner in which the operator is used. 9
  10. 10. INTRODUCTION Some operators are overloaded frequently, especially operators like + and —. The job performed by overloaded operators also can be performed by explicit method calls, but operator notation is often clearer. 10
  11. 11. STRING REPRESENTATION A Python class can define special method __str__, to provide an informal (i.e., human-readable) string representation of an object of the class. __str__ is analogous to Java’s toString() method. 11
  12. 12. STRING REPRESENTATION If a client program of the class contains the statement print objectOfClass Python calls the object’s __str__ method and outputs the string returned by that method. 12
  13. 13. STRING REPRESENTATION The raw face of an object. 13
  14. 14. STRING REPRESENTATION default invocation >> print halimawSaBanga __main__.HalimawSaBanga instance at 0x0204E120 if __str__ is overriden >> print halimawSaBanga 500-year-old na Halimaw natagpuan sa banga! 14
  15. 15. The Raw Faces of an Object The __str__ Faces of an Object 15
  16. 16. STRING REPRESENTATION 16
  17. 17. STRING REPRESENTATION 17
  18. 18. ATTRIBUTE ACCESS 18
  19. 19. ATTRIBUTE ACCESS 19
  20. 20. ATTRIBUTE ACCESS 20
  21. 21. ATTRIBUTE ACCESS 21
  22. 22. ATTRIBUTE ACCESS 22
  23. 23. ATTRIBUTE ACCESS 23
  24. 24. OPERATOR OVERLOADING Python does not allow new operators to be created, it does allow most existing operators to be overloaded when these operators are used with objects of a programmer-defined type, the operators have meaning appropriate to the new types. 24
  25. 25. Overloading: It’s about maximizing capabilities.
  26. 26. OPERATOR OVERLOADING overloading contributes to the extensibility of Python language 26
  27. 27. OPERATOR OVERLOADING Restrictions 1. precedence cannot be changed 2. arity cannot be changed (i.e., unary to binary) 27
  28. 28. OPERATOR OVERLOADING Restrictions 28
  29. 29. OPERATOR OVERLOADING Unary A unary operator for a class is overloaded as a method that takes only the object reference argument (self ) 29
  30. 30. OPERATOR OVERLOADING Unary 30
  31. 31. OPERATOR OVERLOADING Binary A binary operator or statement for a class is overloaded as a method with two arguments: self and other. 31
  32. 32. OPERATOR OVERLOADING Rational A Rational number is a fraction represented as a numerator (top) and a denominator (bottom). A rational number can be positive, negative or zero. Class Rational’s interface includes a default constructor, string representation method, overloaded abs function, equality operators and several mathematical operators. 32
  33. 33. OPERATOR OVERLOADING Rational 33
  34. 34. OPERATOR OVERLOADING Binary When overloading binary operator +, if y and z are objects of class Rational, then y + z is treated as if y.__add__(z) had been written, invoking the __add__ method. 34
  35. 35. OPERATOR OVERLOADING Binary Usually, overloaded binary operator methods create and return new objects of their corresponding class. 36
  36. 36. OPERATOR OVERLOADING Binary What happens if we evaluate the expression y + z or the statement y += z, and only y is an object of class Rational? In both cases, z must be coerced (i.e., converted) to an object of class Rational, before the appropriate operator overloading method executes. 37
  37. 37. OPERATOR OVERLOADING Binary 38
  38. 38. OPERATOR OVERLOADING Binary 39
  39. 39. OPERATOR OVERLOADING Built-ins A class also may define special methods that execute when certain built-in functions are called on an object of the class. For example, we may define special method __abs__ for class Rational, to execute when a program calls abs(rationalObject) to compute the absolute value of an object of that class. 40
  40. 40. OPERATOR OVERLOADING Built-ins 41
  41. 41. TYPE CONVERSION Sometimes all the operations “stay within a type.” For example, adding (concatenating) a string to a string produces a string. But, it is often necessary to convert or coerce data of one type to data of another type. 42
  42. 42. TYPE CONVERSION Programmers can force conversions among built-in types by calling the appropriate Python function, such as int or float. 43
  43. 43. TYPE CONVERSION But what about user-defined classes? The interpreter cannot know how to convert among userdefined classes and built-in types. 44
  44. 44. TYPE CONVERSION The programmer must specify how such conversions are to occur with special methods that override the appropriate Python functions. 45
  45. 45. TYPE CONVERSION For example, a class can define special method __int__ that overloads the behavior of the call int( anObject ) to return an integer representation of the object. 46
  46. 46. TYPE CONVERSION 47
  47. 47. TYPE CONVERSION 48
  48. 48. CASE STUDY: RATIONAL CLASS Sample computations with Rational objects: 49
  49. 49. CASE STUDY: RATIONAL CLASS function gcd() computes the greatest common divisor of two values. Class Rational uses this function to simplify the rational number. 50
  50. 50. CASE STUDY: RATIONAL CLASS 51
  51. 51. CASE STUDY: RATIONAL CLASS 52
  52. 52. CASE STUDY: RATIONAL CLASS 53
  53. 53. CASE STUDY: RATIONAL CLASS 54
  54. 54. CASE STUDY: RATIONAL CLASS 55
  55. 55. CASE STUDY: RATIONAL CLASS 56
  56. 56. CASE STUDY: RATIONAL CLASS 57
  57. 57. CASE STUDY: RATIONAL CLASS 58
  58. 58. CASE STUDY: RATIONAL CLASS 59
  59. 59. CASE STUDY: RATIONAL CLASS 60
  60. 60. CASE STUDY: RATIONAL CLASS 61
  61. 61. CASE STUDY: RATIONAL CLASS 62
  62. 62. CASE STUDY: RATIONAL CLASS 63
  63. 63. CASE STUDY: RATIONAL CLASS 64
  64. 64. CASE STUDY: RATIONAL CLASS 65
  65. 65. CASE STUDY: RATIONAL CLASS 66
  66. 66. CASE STUDY: RATIONAL CLASS 67
  67. 67. END NOTES Overloading is a powerful concept. It allows the programmer to reuse/modify existing concepts and operations. 68
  68. 68. …and enables the customization of classes. SEQUENCE OVERLOADING
  69. 69. SEQUENCE OVERLOADING However, too much overloading may also backfire. 
  70. 70. REFERENCES  Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).  Disclaimer: Most of the images/information used here have no proper source citation, and I do not claim ownership of these either. I don’t want to reinvent the wheel, and I just want to reuse and reintegrate materials that I think are useful or cool, then present them in another light, form, or perspective. Moreover, the images/information here are mainly used for illustration/educational purposes only, in the spirit of openness of data, spreading light, and empowering people with knowledge.  71

×