[Tutor] Inheritance Question [calling a superclass's constructor]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 24 Feb 2002 19:13:00 -0800 (PST)


On Sun, 24 Feb 2002, Joel Ricker wrote:

> 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)       

Ah!  I see that you're trying to call the constructor of the superclass.
Try:


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

instead.


Let's take a look again at what was happening before:

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

In this code, our __init__ just instantiated a POP3 instance, and then
just tossed it aside.  It has a similar effect to:

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

so it wasn't really getting our subclass instance primed up for POP work.


Good luck!