PYTHON
24
Person py
Guest on 5th August 2022 03:00:12 PM
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Creates records of personal data.
Person is an example of an ADT (abstract data type). The main feature
is that the construction hides the actual representation of the person
object. In this case the concrete object structure is a list with two
elements, but it does not show in the code using this module. See the
example code at the end.
Excerise: change the underlying structure ot a tuple. Verify that the
test code still works as it should. Also, add a third attribute \"city
of birth\" and write some test code for the new version of the module.
"""
# TODO: comments incomplete for functions
def create(name, age):
return [name, age]
def set_name(p, name):
p[0] = name
def get_name(p):
return p[0]
def set_age(p, name):
p[0] = name
def get_age(p):
return p[1]
def has_same_name(p1, p2):
""" Tests if names are the same, ignores case"""
return (get_name(p1).lower() ==
get_name(p2).lower())
def has_same_age(p1, p2):
""" Tests if ages are the equal."""
return get_age(p1) == get_age(p2)
# Test code
if __name__ == '__main__':
p1 = create("Micke", 44)
p2 = create("Bosse", 44)
p3 = create("Karin", 30)
p4 = create("Micke", 30)
print has_same_name(p1, p2)
print has_same_age(p1, p2)
print has_same_name(p1, p4)