python: object oriented programming - 2pkalra/col100/slides/15-object-part2.pdf · python: object...
TRANSCRIPT
![Page 1: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/1.jpg)
Python: Object Oriented Programming - 2
![Page 2: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/2.jpg)
Recap
• Object Orientation – merge data and functions (that operate on the
data) together into classes – class is like a blue print of an object – objects are instances of a class – typically two kinds of members in a class
• members that store data are attributes • members that are functions are methods
![Page 3: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/3.jpg)
Exmple 1: Class Coordinate
Keyword to indicate declaration of a class
![Page 4: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/4.jpg)
Exmple 1: Class Coordinate
Name of a class
![Page 5: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/5.jpg)
Exmple 1: Class Coordinate
Parent class
![Page 6: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/6.jpg)
Exmple 1: Class Coordinate
special method constructor
![Page 7: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/7.jpg)
Exmple 1: Class Coordinate
method distance
![Page 8: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/8.jpg)
Equivalent
Exmple 1: Class Coordinate
new object of type Coordinate with initial attributes
![Page 9: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/9.jpg)
Operator Overloading
What the operator does, depends on the objects it operates on. For example: >>> a = "Hello "; b = "World" >>> a + b # concatenation 'Hello World' >>> c = 10; d = 20 >>> c + d # addition 30 This is called operator overloading because the operation is overloaded with more than one meaning.
![Page 10: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/10.jpg)
Exmple 2: Class Fraction
![Page 11: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/11.jpg)
Methods: set and get
A well designed class provides methods to get and set attributes. • These methods define the interface to that class. • This allows to perform error checking when values are set, and to hide the implementation of the class from the user.
![Page 12: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/12.jpg)
Methods: set and get
![Page 13: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/13.jpg)
Methods: set and get
![Page 14: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/14.jpg)
Class Hierarchy
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/
![Page 15: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/15.jpg)
Class Inheritance
Sometimes, we need classes that share certain (or very many, or all) attributes but are slightly different. • Example 1: Geometry
a point (in 2 dimensions) has an x and y attribute a circle is a point with a radius a cylinder is a circle with a height
• Example 2: People at universities A person has an address. A student is a person and selects modules. A lecturer is a person with teaching duties.
• In these cases, we define a base class and derive other classes from it. • This is called inheritance.
![Page 16: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/16.jpg)
Class Inheritance
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/
![Page 17: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/17.jpg)
Class Inheritance
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/
![Page 18: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/18.jpg)
Class Inheritance
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/
![Page 19: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/19.jpg)
Class Inheritance: Another Example
Source:Computational Science and Engineering in Python by Hans Fangohr, Engineering and Environment, University of Southampton, United Kingdom
![Page 20: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/20.jpg)
Class Inheritance: Another Example
Source:Computational Science and Engineering in Python by Hans Fangohr, Engineering and Environment, University of Southampton, United Kingdom
![Page 21: Python: Object Oriented Programming - 2pkalra/col100/slides/15-Object-part2.pdf · Python: Object Oriented Programming - 2 . Recap • Object Orientation – merge data and functions](https://reader033.vdocuments.mx/reader033/viewer/2022051107/5ff433b65b9e3604653e1c10/html5/thumbnails/21.jpg)
Class Inheritance: Another Example
Source:Computational Science and Engineering in Python by Hans Fangohr, Engineering and Environment, University of Southampton, United Kingdom