Skip to content

Instantly share code, notes, and snippets.

@evfro
Last active August 10, 2016 11:02
Show Gist options
  • Save evfro/f63d0e187c2f4a6e79181ce9eac510e5 to your computer and use it in GitHub Desktop.
Save evfro/f63d0e187c2f4a6e79181ce9eac510e5 to your computer and use it in GitHub Desktop.
Base Class inheritance problem
# follow up on
# https://medium.com/@cscalfani/goodbye-object-oriented-programming-a59cda4c0e53#.5os988r69
# this, however, is a bit "artificial" example (as it can be simply solved by not defining derived add_all method)
# more realistic scenario would be something like
# http://stackoverflow.com/questions/20822850/change-python-mro-at-runtime
class Array(object):
def __init__(self):
self._a = []
def add(self, element):
self._a.append(element)
def add_all(self, elements_list):
for i in elements_list:
#self._a.append(i) #this will work fine
self.add(i) #this will fail the derived class
class ArrayCount(Array):
def __init__(self):
super(ArrayCount, self).__init__()
self._count = 0
def add(self, element):
super(ArrayCount, self).add(element)
self._count += 1
def add_all(self, elements_list):
super(ArrayCount, self).add_all(elements_list)
self._count += len(elements_list)
if __name__ == '__main__':
arr = ArrayCount()
n = 4
arr.add_all(range(n))
assert arr._count == n, 'Expected {}, got {}'.format(n, arr._count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment