[Tutor] Question about Classes

mlong at datalong.com mlong at datalong.com
Mon Nov 24 23:14:09 EST 2003

Hi Magnus,

> Hi Mike!
> One of the first thing you should know about classes is that
> inheritance means "is-a". In other words, by letting "Contact"
> inherit "Email" as you did in your code, you are claiming that
> a Contact is a kind of Email. I don't think you really see the
> world like that. I certainly don't. ;) This kind of misuse of
> class mechanisms will only cause grief.

Thanks for "is-a" explanation. My gut told me that this would be some sort of abuse
of classes but I am new to OO programing and really don't "grok" it yet. I
rationalize my abuse by calling it a learning exercise. :)

> A contact might *have* emails. That implies composition, or
> an aggregate, that is, the person could have an emails attribute.

So if I wanted to reuse the email functionality in another type of object how would
I do this? For example if have a contact(person entity) class as well as a
company(business entity) class.

> Since the Email class seems to collect emails (plural), it seems
> it would be better to call is "Emails". If you use it through
> an "emails" attribute in the Contact, it seems a bit redundant
> to repeat the word "email" in the method names. (I think this
> long names just occured because your mistake with inheritance.)
> class Emails:
>     def __init__(self):
>         self._emails=[]
>     def show(self):
>          return self._emails
>     def add(self, email, type):
>         self._emails.append((type, email))
>> class Contact(Email):
>> 	def __init__(self
>> 		,firstName=''
>> 		,lastName=''
>> 		):
>> 		""" Initial object """
>> 		self.firstName=firstName
>> 		self.lastName=lastName
>                  self.emails = Emails()
>> 	def updateContact(self
>> 		,firstName
>> 		,lastName
>> 		):
>> 		""" Save Contact Information """
>> 		self.firstName=firstName
>> 		self.lastName=lastName
>> 	def showContactInfo(self):
>> 		return self.firstName + ' ' + self.lastName
> Now you can do:
> p.emails.add('x', 'yyy')
> and
> p.emails.show()

You lost me here. In the preceding section you define _email and yet you refer to
email in this last bit. Does the underscore perform some sort of magic?


More information about the Tutor mailing list