This Week's Worksheet⬇️

mentor08.pdf

mentor08_sol.pdf

OOP Notes

Custom addable __add__

class Rational:
    def __init__(self, numerator, denominator):
        g = gcd(numerator, denominator)
        self.numer = numerator // g
        self.denom = denominator // g

    def __add__(self, other):
        new_numer = self.numer * other.denom + other.numer * self.denom
        new_denom = self.denom * other.denom
        return Rational(new_numer, new_denom)

Inheritance, overriding init —> super().__init__(), do not include 'self'

getattr(object, name, (default))

class Light(object):

    def __init__(self, brightness):
        self.brightness = brightness

    def __getattribute__(self, name):
        print('__getattribute__ ', name)
        return super().__getattribute__(name)

hassattr(object, name)

Iterator / Generator Notes

Iterables

Iterators

Handling StopIteration