fork download
  1. class Person():
  2. def __init__(self, name, age=None, friends=None):
  3. self.name = name
  4. self.age = age
  5. if friends is None:
  6. friends = []
  7. self.friends = PersonFriendList(friends)
  8.  
  9.  
  10. class PersonFriendList(list):
  11. def __init__(self, *args):
  12. super(PersonFriendList, self).__init__(*args)
  13. self.DebugPrint('constructed with {}'.format(str(*args)))
  14.  
  15. def DebugPrint(self, string):
  16. print('{}(): {}'.format(self.__class__.__name__, string))
  17.  
  18. def append(self, *args):
  19. super(PersonFriendList, self).append(*args)
  20. self.DebugPrint('appending {}'.format(str(*args)))
  21.  
  22. me = Person('Mr. Me')
  23. you = Person('Ms. You')
  24.  
  25. me.age = 42
  26. me.friends.append(you)
  27.  
Success #stdin #stdout 0.1s 10096KB
stdin
Standard input is empty
stdout
PersonFriendList(): constructed with []
PersonFriendList(): constructed with []
PersonFriendList(): appending <__main__.Person object at 0xb73cc50c>