[Tutor] Little subclass understanding problem

Michael H. Goldwasser goldwamh at slu.edu
Thu Nov 15 20:16:53 CET 2007



Oops...

Just finished sending my earlier response and realize that I overlooked an issue.

The code from http://viner.tv/go?set is potentially errant.  When
invoking the base class constructor, self should have been explcitly
sent as a parameter, using the syntax

        class Set(list):
            def __init__(self, value = []):
                list.__init__(self)
                self.concat(value)              # copies mutable default

I had gotten sidetracked by the issue of why value was not being
explicitly sent. This explains in part why the original author used
the syntax 
                list.__init__([])

with an empty list as the parameter to __init__.  That makes such a
command legal, but the call is entirely pointless, as it invokes the
initializer on a different instance than the one that we are currently
responsible for initializing.

I suspect that the code as written is faulty because the internal
state of the list is not necessarily properly initialized.   However,
since list is a built-in class, it is possible that the author of this
code is getting lucky because initialization of a Python list may be
happening by other means than list.__init__

This example is certainly not a proper use of inheritance.

With regard,
Michael



On Thursday November 15, 2007, Michael H. Goldwasser wrote: 

>    
>    On Thursday November 15, 2007, Tom wrote: 
>    
>    >    I am trying to understand what happens in the following scenario:
>    >    
>    >    class Sub_class(Base_class):
>    >        def __init__(self, data):
>    >            Base_class.__init__(self, data)
>    >    
>    >    as in:
>    >    
>    >    # snippet from http://viner.tv/go?set
>    >    class Set(list):
>    >        def __init__(self, value = []):
>    >            list.__init__([])
>    
>    
>    Tom,
>    
>      Indeed, that first line of the Set constructor body is invoking the
>      constructor for the base class (list, in this case).  The reason
>      that the value parameter is NOT being directly sent to the base
>      class is that there is a desire to avoid allowing potentially
>      duplicate elements into something that is representing a set. 
>    
>      However, it was entirely unnecessary for them to send an empty list
>      as a parameter.  It would suffice to have written
>    
>                 list.__init__()
>    
>      It is important to have the call to the base class initializer
>      because we need to allow for the internal state of the underlying
>      list to be properly intitialized. 
>    
>      Please note that the original source for this from viner.tv
>      has an important fourth line:
>    
>        class Set(list):
>            def __init__(self, value = []):
>                list.__init__([])
>                self.concat(value)              # copies mutable default
>    
>      That fourth line uses a custom method defined later to insert
>      designated values into the set while making sure to avoid
>      duplicates.
>    
>    With regard,
>    Michael
>    
>    _______________________________________________
>    Tutor maillist  -  Tutor at python.org
>    http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list