[Tutor] Question about Classes

mlong at datalong.com mlong at datalong.com
Mon Nov 24 14:08:19 EST 2003


In an effort to understand how classes work I have created 2 classes. After
importing the class I get the following error:

>>> reload(Contact)
<module 'Contact' from 'Contact.pyc'>
>>> p=Contact.Contact(firstName='Joe', lastName='Blow')
>>> p.showContactInfo()
'Joe Blow'
>>> p.addEmail('mlong','datalong')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "Contact.py", line 10, in addEmail
    self.emails.append(seq)
AttributeError: Contact instance has no attribute 'emails'

My question is how do I initialize the variables in the class Email?


Here are the class definitions:

class Email:
	def __init__(self):
		self.emails=[]

	def showEmail(self):
		return self.emails

	def addEmail(self, email, type):
		seq = (type, email)
		self.emails.append(seq)


class Contact(Email):
	def __init__(self
		,firstName=''
		,lastName=''
		):
		""" Initial object """
		self.firstName=firstName
		self.lastName=lastName

	def updateContact(self
		,firstName
		,lastName
		):
		""" Save Contact Information """
		self.firstName=firstName
		self.lastName=lastName

	def showContactInfo(self):
		return self.firstName + ' ' + self.lastName


Thanks,
Mike



More information about the Tutor mailing list