function和bound method的區別
Method被呼叫時,self會自動加到函式列表首位;
Method就是封裝了一個func和一個class object;
Function的__get__可以把自己和一個class object封裝到一個bounded method裡面去;
Python’s object oriented features are built upon a function based environment. Using non-data descriptors, the two are merged seamlessly.
Functions stored in class dictionaries get turned into methods when invoked. Methods only differ from regular functions in that the object instance is prepended to the other arguments. By convention, the instance is called self but could be called this or any other variable name.
Methods can be created manually with types.MethodType
which is roughly equivalent to:
class MethodType: "Emulate Py_MethodType in Objects/classobject.c" def __init__(self, func, obj): self.__func__ = func self.__self__ = obj def __call__(self, *args, **kwargs): func = self.__func__ obj = self.__self__ return func(obj, *args, **kwargs)
To support automatic creation of methods, functions include the __get__()
method for binding methods during attribute access. This means that functions are non-data descriptors that return bound methods during dotted lookup from an instance. Here’s how it works:
class Function: ... def __get__(self, obj, objtype=None): "Simulate func_descr_get() in Objects/funcobject.c" if obj is None: return self return MethodType(self, obj)
Running the following class in the interpreter shows how the function descriptor works in practice:
class D: def f(self, x): return x
The function has a qualified name attribute to support introspection:
>>>
>>> D.f.__qualname__ 'D.f'
Accessing the function through the class dictionary does not invoke __get__()
. Instead, it just returns the underlying function object:
>>>
>>> D.__dict__['f'] <function D.f at 0x00C45070>
Dotted access from a class calls __get__()
which just returns the underlying function unchanged:
>>>
>>> D.f <function D.f at 0x00C45070>
The interesting behavior occurs during dotted access from an instance. The dotted lookup calls __get__()
which returns a bound method object:
>>>
>>> d = D() >>> d.f <bound method D.f of <__main__.D object at 0x00B18C90>>
Internally, the bound method stores the underlying function and the bound instance:
>>>
>>> d.f.__func__ <function D.f at 0x1012e5ae8> >>> d.f.__self__ <__main__.D object at 0x1012e1f98>
If you have ever wondered where self comes from in regular methods or where cls comes from in class methods, this is it!
相關文章
- Python: 函式與方法的區別 以及 Bound Method 和 Unbound MethodPython函式
- 方法(method)和函式(function)有什麼區別?函式Function
- 請教AbstractFactory 和 Factory method 的區別
- $(function(){})與(function($){....})(jQuery)的區別FunctionjQuery
- vue中computed/method/watch的區別Vue
- method=post/get區別記錄
- Function 與 Classes 元件的區別在哪?Function元件
- orm的method屬性值post和get的區別簡單介紹ORM
- [譯] React 是如何區分 Class 和 Function 的 ?ReactFunction
- rust-quiz:018-method-or-function-pointer.rsRustUIFunction
- ../和./和/的區別
- 和 的區別
- as 和 with的區別
- (譯)React是如何區分Class和Function?ReactFunction
- Function型別Function型別
- LinkedList和ArrayList的區別、Vector和ArrayList的區別
- http和https的區別/get和post的區別HTTP
- Python-unsupported operand type(s) for %: 'builtin_function_or_method' and 'int'PythonUIFunction
- unity GPU bound or CPU boundUnityGPU
- ./ 和sh 的區別
- JQuery this和$(this)的區別jQuery
- jquery $(this) 和this的區別jQuery
- T和?的區別
- ++a和a++的區別
- makefile =和:=的區別
- Mybatis中#{}和${}傳參的區別及#和$的區別小結MyBatis
- 和區別
- MYSQL和SQL的區別MySql
- varchar和char的區別
- &self 和 self 的區別
- var和public的區別
- filter和interceptor的區別Filter
- useEffect 和 useLayoutEffect 的區別
- SDK和API的區別?API
- var 和 let 的區別
- WebApi和MVC的區別WebAPIMVC
- service和systemctl的區別
- GET和POST的區別?