use of super()
Jan Hudec
bulb at ucw.cz
Thu Jan 5 09:18:14 GMT 2006
On Thu, Jan 05, 2006 at 15:04:59 +1100, Robert Collins wrote:
> On Wed, 2006-01-04 at 21:10 -0600, John Arbash Meinel wrote:
> > Robert Collins wrote:
> > > I just ran into the following:
> > >
> > > def setUp(self):
> > > super(self.__class__, self).setUp()
> > >
> > > This does not work - it results in the wrong code when further
> > > subclassing occurs.
> > >
> > > super must always be 'super(THECURRENTCLSASNAME, self).method()'
> > >
> > > Rob
> >
> > Where was that done?
>
> test_lockable_files.py. It might even have been me that did it on some
> notion of crack.
>
> > I actually thought we couldn't use super() in test classes, because the
> > base level TestCase is not a child of object. and super() is only
> > defined for new-style python classes.
>
> unittest.py sets __metaclass__=object, which makes the base TestCase a
> child of object. Yes, thats confusing, which is why I hope we do not
> start doingthat
__metaclass__=type, no?
> > Otherwise I'm curious why self.__class__ wouldn't work, it seems like it
> > would be a cute way to get the right class. (Though if it really worked,
> > then I would have thought the interface would be the even nicer
> > super(self).setUp())
>
> Because __class__ is the class of the concrete instance. So if you have
>
> object
> ->Class1
> ->Class2
>
> then in Class1.setUp() reference self.__class__ you will get Class2, and
> thus call Class1.setUp() -> an infinite loop (at best).
Or in short, because the first argument of super is the class where the
method calling it is defined (so it knows how far in the MRO it already
is to be able to contiinue to the next step).
--
Jan 'Bulb' Hudec <bulb at ucw.cz>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20060105/ab42127c/attachment.pgp
More information about the bazaar
mailing list