Strange error with unbound method
Jeff Epler
jepler at inetnebr.com
Tue Apr 17 20:07:31 EDT 2001
On Mon, 16 Apr 2001 18:26:17 +0200, Fernando RodrÃguez
<spamers at must.die> wrote:
> TypeError: unbound method must be called with class instance 1st argument
I see you solved your problem, but asked for a more helpful error message.
I suggest that the replacement might function like the following:
class UnboundInstanceMethod(TypeError):
def __init__(self, arg):
if type(arg) == types.InstanceType:
self.str = "instance of %s" % arg.__class__.__name__
else:
self.str = type(arg).__name__
def __str__(self):
return ("unbound method must be called with class "
"instance 1st argument, not %s" % self.str)
>>> raise UnboundInstanceMethod, 1
Traceback (most recent call last):
File "<stdin>", line 1, in ?
__main__.UnboundInstanceMethod: unbound method must be called with class instance 1st argument, not int
>>> raise UnboundInstanceMethod, X()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
__main__.UnboundInstanceMethod: unbound method must be called with class instance 1st argument, not instance of X
Of course, this can't be the implementation, since this exception is
raised from C. But the principle applies.
Maybe 'unbound method of class %s called with %s first argument' is a
better string --- it's somewhat shorter, and it tells you the class the
method exists on too.
Jeff
More information about the Python-list
mailing list