Create a contact book

Tim Chase python.list at
Tue Oct 26 14:27:04 EDT 2021

On 2021-10-25 22:40, anders Limpan wrote:
> i would like to create a contact book were you can keep track of
> your friends. With this contact book you will both be able to add
> friends and view which friends that you have added. anyone
> interested in helping me out with this one ?=) --

Python provides the shelve module for just this sort of thing:

  import shelve

  class Contact:
      def __init__(self, name):
 = name
          self.address = ""
 = ""

  with"contacts") as db:
      dave =  Contact("Dave Smith")
      dave.address = "123 Main St\nAnytown, NY 12345" = "800-555-1212"
      db["dave"] = dave
      ellen = Contact("Ellen Waite") = "+1234567890"
      db["ellen"] = ellen

Then at some future point you can use

  with"contacts") as db:
      dave = db["dave"]
      print(f"Dave lives at {dave.address}")
      ellen = db["ellen"]
      print(f"Ellen's phonenumber is {ellen.phonenumber}")

I'll leave to you the details of implementing an actual address-book
out of these parts.  Be sure to read the docs for the shelve module 

including the various security warnings and caveats.


More information about the Python-list mailing list