python 方法包装器的 'wrapped' 是什么
What is 'wrapped' by a python method wrapper
我正在通读 this blog post on OOP in python 3。其中有:
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.
我试图更直观地理解这一点。在这种情况下,什么是 'wrapping' 什么?
method-wrapper
对象正在包装一个 C 函数。它将一个实例(这里是一个 function
实例,在 C 结构中定义)和一个 C 函数绑定在一起,这样当你调用它时,正确的实例信息就会传递给 C 函数。
它是 method
对象和自定义 class 上的 function
对象的 C API 等价物。给定一个 class Foo
和一个函数 bar
,Foo().bar
产生一个绑定方法,它结合了 Foo()
实例和 Foo.bar
函数,这样在调用时,您可以将该实例作为第一个参数传递(通常称为 self
)。
另见descriptor protocol;这就是定义 __get__
方法及其调用方式的内容。
我正在通读 this blog post on OOP in python 3。其中有:
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.
我试图更直观地理解这一点。在这种情况下,什么是 'wrapping' 什么?
method-wrapper
对象正在包装一个 C 函数。它将一个实例(这里是一个 function
实例,在 C 结构中定义)和一个 C 函数绑定在一起,这样当你调用它时,正确的实例信息就会传递给 C 函数。
它是 method
对象和自定义 class 上的 function
对象的 C API 等价物。给定一个 class Foo
和一个函数 bar
,Foo().bar
产生一个绑定方法,它结合了 Foo()
实例和 Foo.bar
函数,这样在调用时,您可以将该实例作为第一个参数传递(通常称为 self
)。
另见descriptor protocol;这就是定义 __get__
方法及其调用方式的内容。