__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)
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)
iter(iterable)
—> return iteratornext(iterator)
try / except
ranked_chocolates = ("Dark", "Milk", "White")
chocolaterator = iter(ranked_chocolates)
try:
while True:
choco = next(chocolaterator)
print(choco)
except StopIteration:
print("No more left!")