PYTHON   24

Person py

Guest on 5th August 2022 03:00:12 PM

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. Creates records of personal data.
  6.  
  7. Person is an example of an ADT (abstract data type). The main feature
  8. is that the construction hides the actual representation of the person
  9. object. In this case the concrete object structure is a list with two
  10. elements, but it does not show in the code using this module. See the
  11. example code at the end.
  12.  
  13. Excerise: change the underlying structure ot a tuple. Verify that the
  14. test code still works as it should. Also, add a third attribute \"city
  15. of birth\" and write some test code for the new version of the module.
  16. """
  17.  
  18. # TODO: comments incomplete for functions
  19.  
  20. def create(name, age):
  21.     return [name, age]
  22.  
  23. def set_name(p, name):
  24.     p[0] = name
  25.  
  26. def get_name(p):
  27.     return p[0]
  28.  
  29. def set_age(p, name):
  30.     p[0] = name
  31.  
  32. def get_age(p):
  33.     return p[1]
  34.  
  35. def has_same_name(p1, p2):
  36.     """ Tests if names are the same, ignores case"""
  37.     return (get_name(p1).lower() ==
  38.             get_name(p2).lower())
  39.  
  40. def has_same_age(p1, p2):
  41.     """ Tests if ages are the equal."""
  42.     return get_age(p1) == get_age(p2)
  43.  
  44. # Test code
  45. if __name__ == '__main__':    
  46.     p1 = create("Micke", 44)
  47.     p2 = create("Bosse", 44)
  48.     p3 = create("Karin", 30)
  49.     p4 = create("Micke", 30)
  50.     print has_same_name(p1, p2)
  51.     print has_same_age(p1, p2)
  52.     print has_same_name(p1, p4)

Raw Paste


Login or Register to edit or fork this paste. It's free.