Inheritance problem

Diez B. Roggisch deets at nospam.web.de
Wed Nov 5 18:59:08 CET 2008


Mr.SpOOn wrote:

> Hi,
> I have a problem with this piece of code:
> 
> 
> class NoteSet(OrderedSet):
>     def has_pitch(self):
>         pass
>     def has_note(self):
>         pass
> 
> class Scale(NoteSet):
>     def __init__(self, root, type):
>         self.append(root)
>         self.type = type
>         ScaleType(scale=self)
> 
> OrderedSet is an external to use ordered sets. And it has an append
> method to append elements to the set.
> 
> When I try to create a Scale object:
> 
> s = Scale(n, '1234567')  # n is a note
> 
> I get this error:
> 
> Traceback (most recent call last):
>   File "notes.py", line 276, in <module>
>     s = Scale(n, '1234567')
>   File "notes.py", line 243, in __init__
>     self.append(root)
>   File "ordered_set.py", line 78, in append
>     self._insertatnode(self._end.prev, element)
> AttributeError: 'Scale' object has no attribute '_end'
> 
> I can't understand where the error is.
> Can you help me?

You need to call the __init__ of NoteSet inside Scale, as otherwise the
instance isn't properly initialized.

class Scale(NoteSet):
    def __init__(self, root, type):
        super(Scale, self).__init__()
        ...

or
        NoteSet.__init__(self)

if you have an old-style class.

Diez



More information about the Python-list mailing list