Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. class Base:
  2.  
  3. def __init__(self, a, b, c=None):
  4. self.a = a
  5. self.b = b
  6. self.c = c
  7.  
  8. def __str__(self):
  9. return '{}, {}, {}'.format(self.a, self.b, self.c)
  10.  
  11.  
  12. class Derived(Base):
  13.  
  14. def __init__(self, a, b, c, d):
  15. super().__init__(a, b, c) # pass the to the Base initializer
  16. self.d = d # add the extra variable
  17.  
  18. def __str__(self):
  19. # Here string representation of the Base part with added self.d
  20. return super().__str__() + ', {}'.format(self.d)
  21.  
  22.  
  23. if __name__ == '__main__':
  24.  
  25. bas = Base(1, 2, 3)
  26. print(bas)
  27.  
  28. der = Derived(1, 2, 3, 4)
  29. print(der)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement