blob: 23852251dfe020bdb3da5c2f41eb2abb978a7144 [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
3.. _instancemethod-objects:
4
5Instance Method Objects
6-----------------------
7
8.. index:: object: instancemethod
9
Georg Brandl60203b42010-10-06 10:11:56 +000010An instance method is a wrapper for a :c:data:`PyCFunction` and the new way
11to bind a :c:data:`PyCFunction` to a class object. It replaces the former call
Georg Brandl54a3faa2008-01-20 09:30:57 +000012``PyMethod_New(func, NULL, class)``.
13
14
Georg Brandl60203b42010-10-06 10:11:56 +000015.. c:var:: PyTypeObject PyInstanceMethod_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000016
Georg Brandl60203b42010-10-06 10:11:56 +000017 This instance of :c:type:`PyTypeObject` represents the Python instance
Georg Brandl54a3faa2008-01-20 09:30:57 +000018 method type. It is not exposed to Python programs.
19
20
Georg Brandl60203b42010-10-06 10:11:56 +000021.. c:function:: int PyInstanceMethod_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000022
23 Return true if *o* is an instance method object (has type
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020024 :c:data:`PyInstanceMethod_Type`). The parameter must not be ``NULL``.
Antonio Cuni315fc522021-01-06 12:38:26 +010025 This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000026
27
Georg Brandl60203b42010-10-06 10:11:56 +000028.. c:function:: PyObject* PyInstanceMethod_New(PyObject *func)
Georg Brandl54a3faa2008-01-20 09:30:57 +000029
30 Return a new instance method object, with *func* being any callable object
Ezio Melottie130a522011-10-19 10:58:56 +030031 *func* is the function that will be called when the instance method is
Georg Brandl54a3faa2008-01-20 09:30:57 +000032 called.
33
34
Georg Brandl60203b42010-10-06 10:11:56 +000035.. c:function:: PyObject* PyInstanceMethod_Function(PyObject *im)
Georg Brandl54a3faa2008-01-20 09:30:57 +000036
37 Return the function object associated with the instance method *im*.
38
39
Georg Brandl60203b42010-10-06 10:11:56 +000040.. c:function:: PyObject* PyInstanceMethod_GET_FUNCTION(PyObject *im)
Georg Brandl54a3faa2008-01-20 09:30:57 +000041
Georg Brandl60203b42010-10-06 10:11:56 +000042 Macro version of :c:func:`PyInstanceMethod_Function` which avoids error checking.
Georg Brandl54a3faa2008-01-20 09:30:57 +000043
44
45.. _method-objects:
46
47Method Objects
48--------------
49
50.. index:: object: method
51
52Methods are bound function objects. Methods are always bound to an instance of
Martin Panter6245cb32016-04-15 02:14:19 +000053a user-defined class. Unbound methods (methods bound to a class object) are
Georg Brandl54a3faa2008-01-20 09:30:57 +000054no longer available.
55
56
Georg Brandl60203b42010-10-06 10:11:56 +000057.. c:var:: PyTypeObject PyMethod_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
59 .. index:: single: MethodType (in module types)
60
Georg Brandl60203b42010-10-06 10:11:56 +000061 This instance of :c:type:`PyTypeObject` represents the Python method type. This
Georg Brandl54a3faa2008-01-20 09:30:57 +000062 is exposed to Python programs as ``types.MethodType``.
63
64
Georg Brandl60203b42010-10-06 10:11:56 +000065.. c:function:: int PyMethod_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000066
Georg Brandl60203b42010-10-06 10:11:56 +000067 Return true if *o* is a method object (has type :c:data:`PyMethod_Type`). The
Antonio Cuni315fc522021-01-06 12:38:26 +010068 parameter must not be ``NULL``. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000069
70
Georg Brandl60203b42010-10-06 10:11:56 +000071.. c:function:: PyObject* PyMethod_New(PyObject *func, PyObject *self)
Georg Brandl54a3faa2008-01-20 09:30:57 +000072
73 Return a new method object, with *func* being any callable object and *self*
Ezio Melottie130a522011-10-19 10:58:56 +030074 the instance the method should be bound. *func* is the function that will
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020075 be called when the method is called. *self* must not be ``NULL``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000076
77
Georg Brandl60203b42010-10-06 10:11:56 +000078.. c:function:: PyObject* PyMethod_Function(PyObject *meth)
Georg Brandl54a3faa2008-01-20 09:30:57 +000079
80 Return the function object associated with the method *meth*.
81
82
Georg Brandl60203b42010-10-06 10:11:56 +000083.. c:function:: PyObject* PyMethod_GET_FUNCTION(PyObject *meth)
Georg Brandl54a3faa2008-01-20 09:30:57 +000084
Georg Brandl60203b42010-10-06 10:11:56 +000085 Macro version of :c:func:`PyMethod_Function` which avoids error checking.
Georg Brandl54a3faa2008-01-20 09:30:57 +000086
87
Georg Brandl60203b42010-10-06 10:11:56 +000088.. c:function:: PyObject* PyMethod_Self(PyObject *meth)
Georg Brandl54a3faa2008-01-20 09:30:57 +000089
90 Return the instance associated with the method *meth*.
91
92
Georg Brandl60203b42010-10-06 10:11:56 +000093.. c:function:: PyObject* PyMethod_GET_SELF(PyObject *meth)
Georg Brandl54a3faa2008-01-20 09:30:57 +000094
Georg Brandl60203b42010-10-06 10:11:56 +000095 Macro version of :c:func:`PyMethod_Self` which avoids error checking.