blob: b1862d796c9f4125fb49c0d0d1321b4ab9f3f32a [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``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000025
26
Georg Brandl60203b42010-10-06 10:11:56 +000027.. c:function:: PyObject* PyInstanceMethod_New(PyObject *func)
Georg Brandl54a3faa2008-01-20 09:30:57 +000028
29 Return a new instance method object, with *func* being any callable object
Ezio Melottie130a522011-10-19 10:58:56 +030030 *func* is the function that will be called when the instance method is
Georg Brandl54a3faa2008-01-20 09:30:57 +000031 called.
32
33
Georg Brandl60203b42010-10-06 10:11:56 +000034.. c:function:: PyObject* PyInstanceMethod_Function(PyObject *im)
Georg Brandl54a3faa2008-01-20 09:30:57 +000035
36 Return the function object associated with the instance method *im*.
37
38
Georg Brandl60203b42010-10-06 10:11:56 +000039.. c:function:: PyObject* PyInstanceMethod_GET_FUNCTION(PyObject *im)
Georg Brandl54a3faa2008-01-20 09:30:57 +000040
Georg Brandl60203b42010-10-06 10:11:56 +000041 Macro version of :c:func:`PyInstanceMethod_Function` which avoids error checking.
Georg Brandl54a3faa2008-01-20 09:30:57 +000042
43
44.. _method-objects:
45
46Method Objects
47--------------
48
49.. index:: object: method
50
51Methods are bound function objects. Methods are always bound to an instance of
Martin Panter6245cb32016-04-15 02:14:19 +000052a user-defined class. Unbound methods (methods bound to a class object) are
Georg Brandl54a3faa2008-01-20 09:30:57 +000053no longer available.
54
55
Georg Brandl60203b42010-10-06 10:11:56 +000056.. c:var:: PyTypeObject PyMethod_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000057
58 .. index:: single: MethodType (in module types)
59
Georg Brandl60203b42010-10-06 10:11:56 +000060 This instance of :c:type:`PyTypeObject` represents the Python method type. This
Georg Brandl54a3faa2008-01-20 09:30:57 +000061 is exposed to Python programs as ``types.MethodType``.
62
63
Georg Brandl60203b42010-10-06 10:11:56 +000064.. c:function:: int PyMethod_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000065
Georg Brandl60203b42010-10-06 10:11:56 +000066 Return true if *o* is a method object (has type :c:data:`PyMethod_Type`). The
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020067 parameter must not be ``NULL``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000068
69
Georg Brandl60203b42010-10-06 10:11:56 +000070.. c:function:: PyObject* PyMethod_New(PyObject *func, PyObject *self)
Georg Brandl54a3faa2008-01-20 09:30:57 +000071
72 Return a new method object, with *func* being any callable object and *self*
Ezio Melottie130a522011-10-19 10:58:56 +030073 the instance the method should be bound. *func* is the function that will
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020074 be called when the method is called. *self* must not be ``NULL``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000075
76
Georg Brandl60203b42010-10-06 10:11:56 +000077.. c:function:: PyObject* PyMethod_Function(PyObject *meth)
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
79 Return the function object associated with the method *meth*.
80
81
Georg Brandl60203b42010-10-06 10:11:56 +000082.. c:function:: PyObject* PyMethod_GET_FUNCTION(PyObject *meth)
Georg Brandl54a3faa2008-01-20 09:30:57 +000083
Georg Brandl60203b42010-10-06 10:11:56 +000084 Macro version of :c:func:`PyMethod_Function` which avoids error checking.
Georg Brandl54a3faa2008-01-20 09:30:57 +000085
86
Georg Brandl60203b42010-10-06 10:11:56 +000087.. c:function:: PyObject* PyMethod_Self(PyObject *meth)
Georg Brandl54a3faa2008-01-20 09:30:57 +000088
89 Return the instance associated with the method *meth*.
90
91
Georg Brandl60203b42010-10-06 10:11:56 +000092.. c:function:: PyObject* PyMethod_GET_SELF(PyObject *meth)
Georg Brandl54a3faa2008-01-20 09:30:57 +000093
Georg Brandl60203b42010-10-06 10:11:56 +000094 Macro version of :c:func:`PyMethod_Self` which avoids error checking.
Christian Heimesa156e092008-02-16 07:38:31 +000095
96
Georg Brandl60203b42010-10-06 10:11:56 +000097.. c:function:: int PyMethod_ClearFreeList()
Christian Heimesa156e092008-02-16 07:38:31 +000098
99 Clear the free list. Return the total number of freed items.
100