[IronPython] Method invocation when assigned to instancediffers from default assignment?

Erzengel des Lichtes erzengel-von-licht at cox.net
Sat Feb 17 20:50:57 CET 2007


-----Original Message-----
From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Nicholas Riley
Sent: Saturday, February 17, 2007 2:39 AM
To: Discussion of IronPython
Subject: Re: [IronPython] Method invocation when assigned to instancediffers
from default assignment?

You've discovered the difference between bound and unbound methods.
You can see it if you examine a method when it's accessed via a class and
its instance:

>>> class C(object):
...     def meth(self): return 5
>>> inst = C()
>>> C.meth
<unbound method C.meth>
>>> inst.meth
<bound method C.meth of <C object at 0x000000000000002B>>
>>> C.meth()
Traceback (most recent call last):
TypeError: meth() takes exactly 1 argument (0 given)
>>> C.meth(inst)
>>> inst.meth()

Unbound methods require you specify the instance as an argument, typically
named 'self'; bound methods have an instance already bound to 'self', hence
the name.  When you define a method on a class, what actually gets stored in
the class is a "method descriptor" which dispenses either a bound or unbound
method based on the object (instance or class, respectively) whose attribute
is being looked up.

There's some more information on how lookup works (in CPython, so the
internals aren't quite valid for IronPython, but the behavior is the
same) in PEP 252:


OK, looking into my original code, Test.Func1 was a Function, Test.Func2 was
a Bound Method, and MyClass.Func2 was an unbound method.
My question then is: Why is Test.Func1 not a bound method?

More information about the Ironpython-users mailing list