[Tutor] Question about Classes
Gustavo Tabares
gustabares at verizon.net
Mon Nov 24 16:32:50 EST 2003
Matt,
By subclassing the class "Email", you are overwriting the __init__ function
in the Email class. This means that the instance of Contact has no clue about
the statement:
self.emails = []
in the Email class. You can try placing this statement in the __init__ of
Contact, or make it all one class, or whatever else you might think of.
Gus
On Monday 24 November 2003 14:08, mlong at datalong.com wrote:
> 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
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list