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``. |
Antonio Cuni | 315fc52 | 2021-01-06 12:38:26 +0100 | [diff] [blame] | 25 | This function always succeeds. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 26 | |
| 27 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 28 | .. c:function:: PyObject* PyInstanceMethod_New(PyObject *func) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 29 | |
| 30 | Return a new instance method object, with *func* being any callable object |
Ezio Melotti | e130a52 | 2011-10-19 10:58:56 +0300 | [diff] [blame] | 31 | *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] | 32 | called. |
| 33 | |
| 34 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 35 | .. c:function:: PyObject* PyInstanceMethod_Function(PyObject *im) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 36 | |
| 37 | Return the function object associated with the instance method *im*. |
| 38 | |
| 39 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 40 | .. c:function:: PyObject* PyInstanceMethod_GET_FUNCTION(PyObject *im) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 41 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 42 | Macro version of :c:func:`PyInstanceMethod_Function` which avoids error checking. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | .. _method-objects: |
| 46 | |
| 47 | Method Objects |
| 48 | -------------- |
| 49 | |
| 50 | .. index:: object: method |
| 51 | |
| 52 | 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] | 53 | 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] | 54 | no longer available. |
| 55 | |
| 56 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 57 | .. c:var:: PyTypeObject PyMethod_Type |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 58 | |
| 59 | .. index:: single: MethodType (in module types) |
| 60 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 61 | This instance of :c:type:`PyTypeObject` represents the Python method type. This |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 62 | is exposed to Python programs as ``types.MethodType``. |
| 63 | |
| 64 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 65 | .. c:function:: int PyMethod_Check(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 66 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 67 | Return true if *o* is a method object (has type :c:data:`PyMethod_Type`). The |
Antonio Cuni | 315fc52 | 2021-01-06 12:38:26 +0100 | [diff] [blame] | 68 | parameter must not be ``NULL``. This function always succeeds. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 69 | |
| 70 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 71 | .. c:function:: PyObject* PyMethod_New(PyObject *func, PyObject *self) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 72 | |
| 73 | 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] | 74 | 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] | 75 | be called when the method is called. *self* must not be ``NULL``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 76 | |
| 77 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 78 | .. c:function:: PyObject* PyMethod_Function(PyObject *meth) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 79 | |
| 80 | Return the function object associated with the method *meth*. |
| 81 | |
| 82 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 83 | .. c:function:: PyObject* PyMethod_GET_FUNCTION(PyObject *meth) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 84 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 85 | Macro version of :c:func:`PyMethod_Function` which avoids error checking. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 86 | |
| 87 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 88 | .. c:function:: PyObject* PyMethod_Self(PyObject *meth) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 89 | |
| 90 | Return the instance associated with the method *meth*. |
| 91 | |
| 92 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 93 | .. c:function:: PyObject* PyMethod_GET_SELF(PyObject *meth) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 94 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 95 | Macro version of :c:func:`PyMethod_Self` which avoids error checking. |