object oriented programming
play

Object-Oriented Programming Ali Taheri Sharif University of - PowerPoint PPT Presentation

Fu Fundamentals of Pr Programming (Py Python) Object-Oriented Programming Ali Taheri Sharif University of Technology Spring 2019 Outline 1. Python Data Types 8. Composition 2. Classes and Objects 9. Mutability 3. Defining Classes


  1. Fu Fundamentals of Pr Programming (Py Python) Object-Oriented Programming Ali Taheri Sharif University of Technology Spring 2019

  2. Outline 1. Python Data Types 8. Composition 2. Classes and Objects 9. Mutability 3. Defining Classes 10.Sameness 4. Working with Objects 11.Copying 5. Customizing Initializer 6. Adding Methods 7. Special Methods 2 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  3. Python Data Types So far we have seen several data types in Python Some of these data types are primitive data types ◦ Integer, Float, String, Boolean Some of them are more complicated ◦ List, Dictionary, Set, File In Python, we can build our own data types ◦ Using the other available types ◦ This is the subject of Object-Oriented programming 3 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  4. Classes and Objects An object is simply a variable A Class is simply a data type of an object We have been using objects! ◦ Just didn’t call them objects For example: str is a data type (or class) ◦ We created objects of type (class) string animal = "cow" fruit = "apple" 4 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  5. Classes and Objects Object vs Variable ◦ Objects may contain variables, called attributes ◦ Objects may contain methods Class vs Type ◦ Programmer can define his own classes ◦ Attributes and methods are defined within class 5 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  6. Defining Classes Class header ◦ Keyword class begins definition ◦ Followed by name of class and colon ( : ) Initializer method __init__ ◦ Executes each time an object is created ◦ Initializes attributes of class Object reference ◦ All methods must at least specify this one parameter ◦ Represents object of class from which a method is called ◦ Called self by convention 6 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  7. Defining Classes 7 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  8. Working with Objects Instantiating objects: p = Point() q = Point() Accessing objects’ attributes: p.x = 3 q.x = p.y + 5 q.y = p.x * q.x print(p.x, p.y, q.x, q.y) 8 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  9. Customizing Initializer 9 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  10. Adding Methods 10 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  11. Adding Methods class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def distance_from_origin(self): return ((self.x**2) + (self.y**2)) ** 0.5 def distance(self, other): dx = self.x - other.x dy = self.y - other.y return (dx**2 + dx**2) ** 0.5 p = Point(2, 6) q = Point(-1, 4) d = p.distance(q) print(d) 11 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  12. Special Methods Operator Method Description Addition + __add__(self, other) Subtraction - __sub__(self, other) Multiplication * __mul__(self, other) Division __truediv__(self, other) / Less than __lt__(self, other) < Less that or equal <= __le__(self, other) Greater than > __gt__(self, other) Greater than or equal __ge__(self, other) >= Equal to __eq__(self, other) == Not equal to != __ne__(self, other) 12 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  13. Special Methods Operator Method Description [index] __getitem__(self, index) Index operator in __contains__(self, value) Check membership len __len__(self) The number of elements str __str__(self) The string representation 13 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  14. Special Methods class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def __str__(self): return '(%d,%d)' % (self.x, self.y) def __getitem__(self, index): if index == 0: return self.x elif index == 1: return self.y else : raise IndexError( 'Point index out of range.' ) >>> p = Point(2,3) >>> print(p) (2,3) >>> print(p[1]) 3 14 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  15. Composition 15 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  16. Mutability 16 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  17. Sameness When dealing with objects, the concept of sameness can be ambiguous ◦ Do the two objects contain the same data? ◦ Do they represent the same object? Shallow Equality ◦ Only compares the references, not the contents of the objects 17 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  18. Sameness The equality operator (==) performs shallow equality check by default on user-defined classes ◦ Unless the __eq__() method is implemented >>> p = Point(2,3) >>> q = Point(2,3) >>> p == q False Deep Equality ◦ Compares the contents of the objects ◦ Must be implemented manually through __eq__() or another custom method 18 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  19. Sameness Deep Equality class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def __eq__(self, other): return (self.x == other.x and self.y == other.y) >>> p = Point(2,3) >>> q = Point(2,3) >>> p == q True 19 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  20. Copying The assignment operator make aliases ◦ Changing one results in changing the other >>> p = Point(2,3) >>> q = p >>> p is q True ◦ What if we want separate duplicates? We can copy objects using the copy module ◦ Shallow copy ◦ Deep copy 20 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  21. Copying The assignment operator make aliases ◦ Changing one results in changing the other >>> p = Point(2,3) >>> q = p >>> q.x = 5 >>> print(p) (5,3) ◦ What if we want separate duplicates? We can copy objects using the copy module ◦ Shallow copy ◦ Deep copy 21 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  22. Copying Shallow Copy ◦ Copies the object, but doesn’t copy the embedded objects unless they are immutable 22 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  23. Copying Deep Copy ◦ Copies the object, and recursively copies the embedded objects class Rectangle: """ A class to manufacture rectangle objects """ def __init__(self, corner, w, h): self.corner = corner self.width = w self.height = h def __eq__(self, other): return (self.corner == other.corner and self.width == other.width and self.height == other.height) 23 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  24. Copying Deep Copy ◦ Copies the object, and recursively copies the embedded objects >>> import copy >>> r = Rectangle(Point(1,4), 7, 2) >>> s = copy.copy(r) >>> r == s True >>> s.corner.x = 5 >>> r == s True >>> t = copy.deepcopy(r) >>> r == t True >>> t.corner.x = 6 >>> r == t False 24 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

Recommend


More recommend