use of super()
Robert Collins
robertc at robertcollins.net
Thu Jan 5 04:04:59 GMT 2006
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
> 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).
Rob
--
GPG key available at: <http://www.robertcollins.net/keys.txt>.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20060105/5a3373fb/attachment.pgp
More information about the bazaar
mailing list