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?
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 function
s 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.