[Tutor] Why super does not work !

Dave Angel davea at ieee.org
Mon Jan 17 23:36:32 CET 2011


On 01/-10/-28163 02:59 PM, Karim wrote:
>
>
> Hello,
>
> I implemented Observer DP on a listbox (Tkinter) as follows and I don't
> understand why super() is not working and Observable.__init__(self) is
> working, cf below:
>
> class ListObservable(Listbox, Observable):
> """Creation de widget Listbox"""
> def __init__(self):
> super(ListObservable, self).__init__()
> #Observable.__init__(self)
> self._value = None
> self.listeContenu = StringVar()
> self.listeContenu.set(' '.join(sorted(data_base.keys())))
> self.liste = Listbox(listvariable=self.listeContenu, selectmode='single')
> self.liste.grid(row=0, column=1, sticky=N+S+W)
> self.liste.bind('<Button-1>', self.onSelect)
>
> _The error is:_
> Traceback (most recent call last):
> File "./observerAppliGraphique.py", line 118, in <module>
> app=App()
> File "./observerAppliGraphique.py", line 37, in __init__
> self.sujet.attach(self.nom)
> File "/home/karim/guiObserver/observable.py", line 11, in attach
> self._observers.append(observer)
> AttributeError: 'ListObservable' object has no attribute '_observers'
>
> And the Observable class is:
>
> class Observable(object):
> """Sujet a observer"""
> def __init__(self):
> self._observers = []
> print('constructeur observable')
>
> def attach(self, observer):
> """Attache un nouvel observateur"""
> self._observers.append(observer)
>
> def detach(self, observer):
> """Retire un nouvel observateur"""
> self._observers.remove(observer)
>
> def notify(self):
> """Avertit tous les observateurs que l'observable change d'etat"""
> for observer in self._observers:
> observer.update()
>
Just looking at what you supply, I'd suppose that ListBox doesn't 
correctly call super() in its own __init__() method.

So I took a look at Tkinter.py, and sure enough, it just calls its own 
direct parent, Widget.l

super() only works if all the classes you use are done consistently.

DaveA



More information about the Tutor mailing list