Previous Lecture Lecture 5 Next Lecture

Lecture 5, Tue 01/23

Pytest, Operator Overloading, Inheritance

Slides folder

Lecture recording

Plan for today

Operator Overloading

What are operators?

We can re-define them to work appropriately with our custom classes, similar to how we overloaded the __eq__ method, which controls the == operator.

Looked at the difference between __str__ and __repr__ methods.

Defining __str__

from Student import Student

s1 = Student("Gaucho", 1234567)
s2 = Student("Jane", 5555555)
print(s1) <Student.Student object at 0x7fd5380d8e80>
def __str__(self):
	''' returns a string representation of a student '''
	return "Student name: {}, perm: {}".format(self.name, self.perm) 

Overriding the ‘+’ operator

def __add__(self, rhs):
	''' Takes two students and returns a list containing these two students '''
    return [self, rhs]
x = s1 + s2 # returns a list of s1 + s2
print(type(x)) # list type

for i in x:
	print(i)

# Output of for loop
# Student name: Gaucho, perm: 1234567
# Student name: Jane, perm: 5555555

Overloading the ‘<=’ and ‘>=’ operator

# <=
def __le__(self, rhs):
	''' Takes two students and returns True if the
		student is less than or equal to the other
		student based on the lexicographical order
		of the name '''
	return self.name.upper() <= rhs.name.upper()

# >=
def __ge__(self, rhs):
	''' Takes two students and returns True if the
		student is greater than or equal to the other
		student based on the lexicographical order
		of the name '''
	return self.name.upper() >= rhs.name.upper()

# >
# def __gt__

# <
# def __lt__
print(s1 <= s2) # True
print(s1 >= s2) # False
print(s1 == s2) # False
print(s1 < s2) # ERROR, we didn’t define the __lt__ method

Checking what’s been defined for an object

Check what’s been defined for an object using a built-in function dir() (check help(dir)).

A student asked: how do we know if a default method or an operator has been overloaded?

using __dict__ - if the default method is listed in that dictionary, then it has been overloaded; otherwise, the default method is used.

A question for the class: Can we create an instance of an object without a constructor?


Testing

Pytest

Write a function getMax(a,b,c,d) that takes 4 int values and returns the largest

# testFile.py

# imports the getMax function from getMax_func.py
from lecture import getMax 

def test_getMax1_position():
    assert getMax(1,2,3,4) == 4
    assert getMax(1,2,4,3) == 4
    assert getMax(1,4,2,3) == 4

def test_getMax2_same():
    assert getMax(5,5,5,5) == 5
    assert getMax(-5,-5,-5,-5) == 5
    # etc.

def test_getMax3():
    assert getMax(-5,-10,-12,-100) == -5
    assert getMax(-100, 1, 100, 0) == 100
    # etc.
# getMax_func.py

def getMax(a, b, c, d):
	currMax = 0
	if a >= b and a >= c and a >= d:
		currMax = a
	if b >= a and b >= c and b >= d:
		currMax = b
	if c >= a and c >= b and c >= d:
		currMax = c
	else:
		currMax = d

Command to run pytest on testFile.py:

Inheritance

Looking at the Inheritance Hierarchy of the classes that you’ll write for lab02,

What about DrinkOrder?