Last active
March 12, 2020 21:49
-
-
Save mineta/10720295 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Day(object): | |
def __init__(self, visits, contacts): | |
self.visits = visits | |
self.contacts = contacts | |
def __add__(self, other): | |
total_visits = self.visits + other.visits | |
total_contacts = self.contacts + other.contacts | |
return Day(total_visits, total_contacts) | |
def __radd__(self, other): | |
if other == 0: | |
return self | |
else: | |
return self.__add__(other) | |
def __str__(self): | |
return "Visits: %i, Contacts: %i" % (self.visits, self.contacts) | |
day1 = Day(10, 1) | |
day2 = Day(20, 2) | |
print day1 | |
# Visits: 10, Contacts: 1 | |
print day2 | |
# Visits: 20, Contacts: 2 | |
day3 = day1 + day2 | |
print day3 | |
# Visits: 30, Contacts: 3 | |
day4 = sum([day1, day2, day3]) | |
print day4 | |
# Visits: 60, Contacts: 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment