Even more objects! - deeper into classes
Step 1
Write a small database for your personal contacts.
Each contact should be represented by a single object1 that can hold information about name, date of birth, phone number and mail address.2
Store your contacts in an appropriate data structure. 3
Step 2
Wouldn’t it be great if you could simply print your contacts by just calling print()
with the identifier of the contact object?
Add a __str__
method4 in your class to make it possible.
Step 3
Imagine one of your contacts get a new mailadress.
Add functions to your class that make you able to change the mail address or phone number of a contact (for practising purposes one function is enough).5
Step 4
Now splice your mail/phone number changer function with some I/O.
Use string formatting to ask the user for the new information while displaying the old.6
-
You could write a class
Contact()
to instantiate objects from it. ↩ -
Hand over this information to the
__init__
function and store them in instance variables. ↩ -
You might want to use a
dict
or alist
. ↩ -
Don’t forget the
self
argument… ↩ -
Keep it simple! Call the function from an object with e.g.
identifier.changeMail("xy@z.com")
and replace the address stored in the instance. ↩ -
You need the
str.format()
function here. You can nest it inside of aninput()
function. ↩