class objects, method objects, function objects

Alex Martelli aleax at mac.com
Mon Mar 19 01:17:39 EDT 2007


7stud <bbxx789_05ss at yahoo.com> wrote:
   ...
> But the last part of the passage makes no sense to me:
> ------
> When the method object is called with an argument list, it is unpacked
> again, a new argument list is constructed from the instance object and
> the original argument list, and the function object is called with
> this new argument list.
> ------
> Can anyone interpret that for me?

Here's the Python pseudocode equivalent...

class Method(object):
  def __init__(self, im_func, im_self):
    self.im_func = im_func
    self.im_self = im_self
  def __call__(self, *a, **k):
    return self.im_func(self.im_self, *a, **k)

Whenever you get foo.somemeth from an instance foo of whatever class
(with somemeth being a method of that class), you obtain the equivalent
of a Method(type(foo).__dict__('somemeth'), foo) in the above
pseudocode.  When you call that foo.somemeth, the im_self (i.e., foo) is
prepended to other positional arguments (if any).

This, btw, takes place in the __get__ method of the function object
somemeth -- function objects are descriptors, i.e., they have __get__
methods, and that's just what they do.

If this is still a bit obscure to you, perhaps more practice with Python
may be needed before you dwell into the internals of methods &c.


Alex



More information about the Python-list mailing list