7

So I know in python everything is an 'object' meaning that it can be passed as an argument to a method. But I'm trying to understand how exactly does this work. So I was trying out the following example:

class A:

    def __init__(self):
        self.value = 'a'

    def my_method(self)
        print self.value

class B:

   def __init__(self):
       self.values = 'b'

   def my_method(self, method):
       method()

a = A()
b = B()
b.my_method(a.my_method)

Now first this was written just to see how things work. I know I should for example check if my_method 's argument is callable. Now my question is:

How exactly is the method passed here? I mean the output I'm recieving is 'a' so I'm guessing that when a object method is passed as parameter so is the actual object ? In this case when I pass a.my_method the instance a is also passed ?

1
  • 2
    If you're working with Python 2, use class A(object): rather than class A: to get "new-style classes" which are generally better and what you should work with. Commented Aug 18, 2011 at 12:38

4 Answers 4

8

When you access a.my_method Python sees that it is an attribute of the class and that A.my_method has a method __get__() so it calls A.my_method.__get__(a), that method creates a new object (the 'bound method') which contains both a reference to A.my_method and a reference to a itself. When you call the bound method it passes the call back through to the underlying method.

This happens every time you call any method whether you call it directly or, as in your code, delay the actual call until later. You can find more description of the descriptor protocol as it is known at http://docs.python.org/reference/datamodel.html#descriptors

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the inputs. Python can be tricky until you get used to it :D
4

How exactly is the method passed here?

This is easily answered: in Python, everything is an object :) Also functions/methods, which are specific function objects which can be passed as parameters.

In this case when I pass a.my_method the instance a is also passed?

Yes, in this case the instance a is also passed, though embedded in the function object and cannot be retrieved. On the contrary you could pass the function itself and supply it with an instance. Consider the following example:

b = B()
func = A.my_method # Attention, UPPERCASE A
func(b)

This would call the A classes method my_method with an instance of B.

2 Comments

Strictly speaking A.my_method is neither the function itself, but an unbound method object. a.my_method is a bound method object. You could retrieve the a instance and the original function from a.my_method if you really really needed to (inspect module), but I don't see a reason to do it here.
I tried your suggestion but only way it worked was if A.my_method was a static method having a obj argument. Otherwise it expects a instance of A.
1

Functions are 'first-class objects' in Python. What that means is that a function is an object as much as a list or an integer. When you define a function, you are actually binding the function object to the name you use in the def.

Functions can be bound to other names or passed as parameters to functions (or stored in lists, or...).

source: http://mail.python.org/pipermail/tutor/2005-October/042616.html

Comments

1

a.my_method returns not exactly the function you defined in the class A (you can get it via A.my_method) but an object called a 'bound method' which contains both the original function (or 'unbound method' in Python 2, IIRC) and the object from which it was retrieved (the self argument).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.