0

I'm reading through this blog post on OOP in python 3. In it there is:

As you can see, a __get__ method is listed among the members of the function, and Python recognizes it as a method-wrapper. This method shall connect the open function to the door1 instance, so we can call it passing the instance alone.

I'm trying to understand this more intuitively. In this context what is 'wrapping' what?

Chris Martin
  • 28,558
  • 6
  • 66
  • 126
user1592380
  • 26,587
  • 62
  • 220
  • 414
  • 1
    Relevant: http://stackoverflow.com/questions/11949808/what-is-the-difference-between-a-function-an-unbound-method-and-a-bound-method – Benjamin Hodgson Mar 14 '16 at 22:12

1 Answers1

2

The method-wrapper object is wrapping a C function. It binds together an instance (here a function instance, defined in a C struct) and a C function, so that when you call it, the right instance information is passed to the C function.

It is the C API equivalent of method objects and functions on a custom class. Given a class Foo with a function bar, Foo().bar produces a bound method, which combines together the Foo() instance and the Foo.bar function, so that when called, you can pass in that instance as the first argument (usually called self).

Also see the descriptor protocol; this is what defines the __get__ method and how it is invoked.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
  • Thanks, So would it be fair to say___get___ 'wraps' a C-function, allowing python the ability to access its functionality? – user1592380 Mar 14 '16 at 22:32
  • Yes, with the understanding that it's purpose is to keep function and object together. No point in calling the C function without something to operate on. – Martijn Pieters Mar 14 '16 at 23:41