Bug with super() and reload()?

Michael Hudson mwh at python.net
Thu Jan 16 15:50:26 CET 2003


Thomas Heller <theller at python.net> writes:

> I stumbled over the following problem with super() and reload().
> 
> Consider this module:
> 
> ---- file mod.py ----
> class X(object):
>     def test(self):
>         print "X.test"
> 
> class Y(X):
>     def test(self):
>         print "Y.test"
>         super(Y, self).test()
> ---- EOF ----
> 
> then, running this code:
> 
> ---- the script ----
> import mod
> 
> Y = mod.Y
> 
> reload(mod)
> 
> y = Y()
> y.test()
> ---- EOF ----
> 
> prints this traceback:
> 
>     Y.test
>     Traceback (most recent call last):
>       File "<stdin>", line 8, in ?
>       File "mod.py", line 8, in test
>         super(Y, self).test()
>     TypeError: super(type, obj): obj must be an instance or subtype of type

This is no different from this old chestnut:

>>> import mod
>>> y = mod.Y()
>>> reload(mod)
<module 'mod' from 'mod.pyc'>
>>> mod.Y.test(y)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unbound method test() must be called with Y instance as first argument (got Y instance instead)

is it?

> Thinking hard, I can understand why this traceback occurs: The object
> 'y' is not an instance of the 'Y' refered to in the source code.

You had to think hard for that?  You can't use Python interactively
much...

> Should this be considered a bug, or is it simply a wart, and reload
> should be avoided?

Well, it's not gonna change anytime soon, so I guess it's a wart.

This may interest you:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164

I could rewrite it to frob __bases__ now, I guess.

Cheers,
M.

-- 
  /* I'd just like to take this moment to point out that C has all
     the expressive power of two dixie cups and a string.
   */                       -- Jamie Zawinski from the xkeycaps source




More information about the Python-list mailing list