Docsity

Python Rectangle Class: Understanding Objects, Methods, and Inheritance, Exams of Object Oriented Programming

Partial preview of the text

Download Python Rectangle Class: Understanding Objects, Methods, and Inheritance and more Exams Object Oriented Programming in PDF only on Docsity!

Lecture 11

More Object-Oriented Programming

based in part on notes from the CS-for-All curriculum developed at Harvey Mudd College

Unrelated but do you see how happy dogs make Milla???

Recall: Our Rectangle Class

rectangle.py

class Rectangle: def init(self, init_width, init_height): self.x = 0 self.y = 0 self.width = init_width self.height = init_height def grow(self, dwidth, dheight): self.width += dwidth self.height += dheight def area(self): return self.width * self.height def perimeter(self): return 2self.width + 2self.height def scale(self, factor): self.width *= factor self.height *= factor

Simplified Client Program from rectangle import *

construct two Rectangle objects

r1 = Rectangle(100, 50) r2 = Rectangle(75, 350)

print dimensions and area of each

print('r1:', r1.width, 'x', r1.height) print('area =', r1.area()) print('r2:', r2.width, 'x', r2.height) print('area =', r2.area())

grow both Rectangles

r1.grow(50, 10) r2.grow(5, 30)

print new dimensions

print('r1:', r1.width, 'x', r1.height) print('r2:', r2.width, 'x', r2.height)

Recall: Our Rectangle Class

rectangle.py

class Rectangle: def init(self, init_width, init_height): self.x = 0 self.y = 0 self.width = init_width self.height = init_height def grow(self, dwidth, dheight): self.width += dwidth self.height += dheight def area(self): return self.width * self.height def perimeter(self): return 2self.width + 2self.height **def scale(self, factor): self.width = factor self.height = factor

What is the output of this program? A. 40 40 40 B. 80 40 40 C. 80 40 80 D. 80 80 80 E. none of these from rectangle import * r1 = Rectangle(40, 75) r2 = Rectangle(40, 75) r3 = r r1.scale(2) print(r1.width, r2.width, r3.width)

What is the output of this program? from rectangle import * r1 = Rectangle(40, 75) r2 = Rectangle(40, 75) r3 = r r1.scale(2) print(r1.width, r2.width, r3.width) height 75 width 40 y 0 x 0 height 75 width 40 y 0 x 0 r r global r

What is the output of this program? from rectangle import * r1 = Rectangle(40, 75) r2 = Rectangle(40, 75) r3 = r r1.scale(2) # changes are still inside the object! print(r1.width, r2.width, r3.width) height 75 width 40 y 0 x 0 height 150 width 80 y 0 x 0 r r global r

What is the output of this program? from rectangle import * r1 = Rectangle(40, 75) r2 = Rectangle(40, 75) r3 = r r1.scale(2) print(r1.width, r2.width, r3.width) height 75 width 40 y 0 x 0 height 150 width 80 y 0 x 0 r r global r output: 80 40 80

What is the output of this client program? from rectangle import * r1 = Rectangle(40, 75) r2 = Rectangle(40, 75) r3 = r print(r1 == r2) # outputs False print(r1 == r3) # outputs True height 75 width 40 y 0 x 0 height 75 width 40 y 0 x 0 r r global r

eq (Implementing Our Own ==)

  • The eq method of a class allows us to implement our own version of the == operator.
  • If we don't write a eq method for a class, we get a default version that compares the object's memory addresses - see the previous example!

repr (Printing/Evaluating an Object)

  • The repr method of a class returns a string representation of objects of that class.
  • It gets called when you:
  • evaluate an object in the Shell: >> r1 = Rectangle(100, 80) >> r1 # calls repr
  • apply str(): >> r1string = str(r1) # also calls repr
  • print an object: >> print(r1) # also calls repr

repr (Printing/Evaluating an Object)

  • If we don't write a repr method for a class, we get a default version that isn't very helpful! >>> r2 = Rectangle(50, 20) >>> r <main.Rectangle object at 0x03247C30>

Updated Rectangle Class class Rectangle: def init(self, init_width, init_height): ... def grow(self, dwidth, dheight): self.width += dwidth self.height += dheight def area(self): return self.width * self.height def perimeter(self): return 2self.width + 2self.height def scale(self, factor): self.width *= factor self.height *= factor def eq(self, other): if self.width == other.width and self.height == other.height: return True return False def repr(self): return str(self.width) + ' x ' + str(self.height)

Simplifying the Client Program Again...

from rectangle import *

# Construct two Rectangle objects

r1 = Rectangle(100, 50)

r2 = Rectangle(75, 350)

# Print dimensions and area of each

print('r1:', r1.width, 'x', r1.height)

print('area =', r1.area())

print('r2:', r2.width, 'x', r2.height)

print('area =', r2.area())

# grow both Rectangles

r1.grow(50, 10)

r2.grow(5, 30)

# Print new dimensions

print('r1:', r1.width, 'x', r1.height)

print('r2:', r2.width, 'x', r2.height)