[Tutor] Inheritance Question

Andy W toodles@yifan.net
Mon, 25 Feb 2002 11:22:40 +0800


Sorry the original isn't prefixed with lots of ">"s, I received it in HTML
and converted to text.

<snippet>

import poplib

class POP3Search(poplib.POP3):
    """Create a connection to a POP3 mailserver and search for messages"""

    def __init__(self, host = "", port = 110):
        poplib.POP3(host, port)

if __name__ == "__main__":
    s = POP3Search("pop3.mailserver")
    print s.getwelcome()

Now my understanding is -- and I guess I'm wrong -- is that POP3Search will
inherit the attributes of POP3 so that getwelcome() when run will: Look in
the child -- since none found, look in the parent -- executes that and
returns the result.  getwelcome returns self.welcome -- self in this case
being the parent.  Since welcome isn't found in the current class
(POP3Search), find it in the parent (POP3).  Any case I'm getting an error
that its not being found:

 File "C:\PYTHON22\lib\poplib.py", line 172, in getwelcome
    return self.welcome
AttributeError: POP3Search instance has no attribute 'welcome'

</snippet>

At the moment, your subclass does not initialise the parent (poplib.POP3),
but rather it creates a new instance which is lost straight away. I stuck my
nose into poplib.py, and found out that "elf.welcome" is initialised in
__init__.
The following should work, if I'm not mistaken:

class POP3Search(poplib.POP3):
    """Create a connection to a POP3 mailserver and search for messages"""

    def __init__(self, host = "", port = 110):
        poplib.POP3.__init__(self,host, port)

HTH,
Andy

----- Original Message -----
From: Joel Ricker
To: tutor@python.org
Sent: Monday, February 25, 2002 11:02 AM
Subject: [Tutor] Inheritance Question


Ok, I've now played with rfc822 and the mime related modules to get an idea
of how things work and what direction I want to go.  To get a feel for
inheritance, I decided to make up a class called POP3Search which would
inherit from poplib.POP3 (which connects to a pop3 server and queries for
messages) with the extra features of message caching and search.

This is what I have:

import poplib

class POP3Search(poplib.POP3):
    """Create a connection to a POP3 mailserver and search for messages"""

    def __init__(self, host = "", port = 110):
        poplib.POP3(host, port)

if __name__ == "__main__":
    s = POP3Search("pop3.mailserver")
    print s.getwelcome()

Now my understanding is -- and I guess I'm wrong -- is that POP3Search will
inherit the attributes of POP3 so that getwelcome() when run will: Look in
the child -- since none found, look in the parent -- executes that and
returns the result.  getwelcome returns self.welcome -- self in this case
being the parent.  Since welcome isn't found in the current class
(POP3Search), find it in the parent (POP3).  Any case I'm getting an error
that its not being found:

 File "C:\PYTHON22\lib\poplib.py", line 172, in getwelcome
    return self.welcome
AttributeError: POP3Search instance has no attribute 'welcome'

What can I do to fix (and understand this)?
Thanks

Joel