class Person:
'''Represents a person in the addressbook'''
def __init__(self, name, number):
self.name = name
self.number = number
print('{0} added to the addressbook, with the number {1}'\
.format(self.name, self.number))
try:
ab[self.name] = self.number
with open('.ab.data') as f:
pickle.dump(ab, f)
except NameError:
print('Address book not yet created. Creating...')
ab = { self.name : self.number}
with open('.ab.data') as f:
pickle.dump(ab, f)
def __del__(self):
try:
del ab[self.name]
print('{0} was removed from the addressbook'.format(self.name))
with open('.ab.data') as f:
pickle.dump(ab, f)
except NameError:
print('That name is not in the address book')
Don't you have to declare variables to be a member of that class? The problem I seem to be hitting is that I don't understand how to let the user add entries when you have to type variable_name = Person(name, number). If they add another user, it would have to be something like variable_name2 = Person(name, number). I don't know how to get the program to use different variable names. How would you approach this assignment?

New Topic/Question
Reply




MultiQuote





|