Skip to content

Instantly share code, notes, and snippets.

@cesarkawakami
Created January 10, 2013 19:31
Show Gist options
  • Save cesarkawakami/4505062 to your computer and use it in GitHub Desktop.
Save cesarkawakami/4505062 to your computer and use it in GitHub Desktop.
class Number(object):
def __init__(self, value):
self.value = value
def __add__(self, other):
return Average(self.value, 1) + other
def __radd__(self, other):
return Average(self.value, 1) + other
class Average(object):
def __init__(self, value, n):
self.value = value
self.n = n
def __add__(self, other):
if not isinstance(other, Average):
return NotImplemented
new_n = self.n + other.n
new_value = self.n * self.value + other.n * other.value
new_value /= new_n
return Average(new_value, new_n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment