unbound methods

Peter Otten __peter__ at web.de
Sat Aug 12 05:54:29 EDT 2006


brent.chambers at gmail.com wrote:

> I wrote a small class today at work playing with sockets in command
> line windows. When attempting to call the handle function, I get a
> TypeError.  "Unbound method handle() must be called with connection
> instance as first argument (got nothing instead).  Any suggestions
> would be greatly appreciated.  Thanks!

That happens if you call a (normal) method on the class instead of on an
instance.

>>> class A:
...     def method(self, *args): print args
...

This works:

>>> A().method(42)
(42,)

while this doesn't:

>>> A.method(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unbound method method() must be called with A instance as first
argument (got int instance instead)

For details you need to post some code illustrating the problem.

Peter



More information about the Python-list mailing list