Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 2 | |
| 3 | .. _instancemethod-objects: |
| 4 | |
| 5 | Instance Method Objects |
| 6 | ----------------------- |
| 7 | |
| 8 | .. index:: object: instancemethod |
| 9 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 10 | An instance method is a wrapper for a :c:data:`PyCFunction` and the new way |
| 11 | to bind a :c:data:`PyCFunction` to a class object. It replaces the former call |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 12 | ``PyMethod_New(func, NULL, class)``. |
| 13 | |
| 14 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 15 | .. c:var:: PyTypeObject PyInstanceMethod_Type |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 16 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 17 | This instance of :c:type:`PyTypeObject` represents the Python instance |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 18 | method type. It is not exposed to Python programs. |
| 19 | |
| 20 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 21 | .. c:function:: int PyInstanceMethod_Check(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 22 | |
| 23 | Return true if *o* is an instance method object (has type |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 24 | :c:data:`PyInstanceMethod_Type`). The parameter must not be ``NULL``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 25 | |
| 26 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 27 | .. c:function:: PyObject* PyInstanceMethod_New(PyObject *func) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 28 | |
| 29 | Return a new instance method object, with *func* being any callable object |
Ezio Melotti | e130a52 | 2011-10-19 10:58:56 +0300 | [diff] [blame] | 30 | *func* is the function that will be called when the instance method is |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 31 | called. |
| 32 | |
| 33 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 34 | .. c:function:: PyObject* PyInstanceMethod_Function(PyObject *im) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 35 | |
| 36 | Return the function object associated with the instance method *im*. |
| 37 | |
| 38 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 39 | .. c:function:: PyObject* PyInstanceMethod_GET_FUNCTION(PyObject *im) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 40 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 41 | Macro version of :c:func:`PyInstanceMethod_Function` which avoids error checking. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | .. _method-objects: |
| 45 | |
| 46 | Method Objects |
| 47 | -------------- |
| 48 | |
| 49 | .. index:: object: method |
| 50 | |
| 51 | Methods are bound function objects. Methods are always bound to an instance of |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 52 | a user-defined class. Unbound methods (methods bound to a class object) are |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 53 | no longer available. |
| 54 | |
| 55 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 56 | .. c:var:: PyTypeObject PyMethod_Type |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 57 | |
| 58 | .. index:: single: MethodType (in module types) |
| 59 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 60 | This instance of :c:type:`PyTypeObject` represents the Python method type. This |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 61 | is exposed to Python programs as ``types.MethodType``. |
| 62 | |
| 63 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 64 | .. c:function:: int PyMethod_Check(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 65 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 66 | Return true if *o* is a method object (has type :c:data:`PyMethod_Type`). The |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 67 | parameter must not be ``NULL``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 68 | |
| 69 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 70 | .. c:function:: PyObject* PyMethod_New(PyObject *func, PyObject *self) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 71 | |
| 72 | Return a new method object, with *func* being any callable object and *self* |
Ezio Melotti | e130a52 | 2011-10-19 10:58:56 +0300 | [diff] [blame] | 73 | the instance the method should be bound. *func* is the function that will |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 74 | be called when the method is called. *self* must not be ``NULL``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 75 | |
| 76 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 77 | .. c:function:: PyObject* PyMethod_Function(PyObject *meth) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 78 | |
| 79 | Return the function object associated with the method *meth*. |
| 80 | |
| 81 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 82 | .. c:function:: PyObject* PyMethod_GET_FUNCTION(PyObject *meth) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 83 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 84 | Macro version of :c:func:`PyMethod_Function` which avoids error checking. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 85 | |
| 86 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 87 | .. c:function:: PyObject* PyMethod_Self(PyObject *meth) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 88 | |
| 89 | Return the instance associated with the method *meth*. |
| 90 | |
| 91 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 92 | .. c:function:: PyObject* PyMethod_GET_SELF(PyObject *meth) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 93 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 94 | Macro version of :c:func:`PyMethod_Self` which avoids error checking. |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 95 | |
| 96 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 97 | .. c:function:: int PyMethod_ClearFreeList() |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 98 | |
| 99 | Clear the free list. Return the total number of freed items. |
| 100 | |