Exercise: Treating People as Objects
Introduction
In this exercise you will practice defining and using classes, and performing simple manipulations of collections of objects.
Problem Description
You need to view some data about people in various ways.
Solution Description
The people.py
module has starter code that creates Person
objects in a list peeps
. In the people.py
file:
Write a
Person
class with:- an
__init__
method that takes:- a name (str),
- a birthdate (str) formatted in ISO 8601 format,
- a height in cm (int),
- a weight in kilograms (float),
- a
__repr__
method that returns astr
like<name, birthdate, height, weight>
field, e.g.,<Stan, 2008-08-13, 150cm, 45kg>
; - a
height_inches
method that returns thePerson
object’s height in inches (1in = 2.54cm), and - a
weight_pounds
method that returns thePerson
object’s weight in pounds (1kg = 2.2lb). - a
days_unitl
method that takes an age in years and returns the number of days until (or since, as a negative number) this Person turnsage
years old.
- an
Do the following exercises to practice using Person
objects:
Write an expression that assigns to
avg_height
the average height of thePerson
objects inpeeps
.Write an expression that assigns to
avg_weight
the average weight of thePerson
objects inpeeps
.- Bonus: write a function,
avg_by
, that takes a sequence ofPerson
objects and akey
function specifying the attribute ofPerson
objects to take the average of, similar to thekey
function ofsorted
.
- Bonus: write a function,
Write an expression that assigns to
name2height
a Dict[str, int] that mapsPerson
names to their heights.Write an expression that assigns to
name2weight
a Dict[str, int] that mapsPerson
names to their weights.- Bonus: write a function,
dict_builder
, that takes a list of ojects and akey_val
function that takes a single parameter that is an element of the list, and returns a tuple that becomes a key-value mapping in a dict returned bydict_builder
.
- Bonus: write a function,
Write an expression that assigns to
peeps_by_age
a list ofPerson
objects inpeeps
sorted in descending order by age.Write a loop that prints the names and ages until they turn 18 of all the
Person
s inpeeps
.
Sample Answer
Don’t peek until you’ve tried it yourself!