3 .. _allocating-objects:
5 Allocating Objects on the Heap
6 ==============================
9 .. cfunction:: PyObject* _PyObject_New(PyTypeObject *type)
12 .. cfunction:: PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size)
14 .. versionchanged:: 2.5
15 This function used an :ctype:`int` type for *size*. This might require
16 changes in your code for properly supporting 64-bit systems.
19 .. cfunction:: void _PyObject_Del(PyObject *op)
22 .. cfunction:: PyObject* PyObject_Init(PyObject *op, PyTypeObject *type)
24 Initialize a newly-allocated object *op* with its type and initial
25 reference. Returns the initialized object. If *type* indicates that the
26 object participates in the cyclic garbage detector, it is added to the
27 detector's set of observed objects. Other fields of the object are not
31 .. cfunction:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
33 This does everything :cfunc:`PyObject_Init` does, and also initializes the
34 length information for a variable-size object.
36 .. versionchanged:: 2.5
37 This function used an :ctype:`int` type for *size*. This might require
38 changes in your code for properly supporting 64-bit systems.
41 .. cfunction:: TYPE* PyObject_New(TYPE, PyTypeObject *type)
43 Allocate a new Python object using the C structure type *TYPE* and the
44 Python type object *type*. Fields not defined by the Python object header
45 are not initialized; the object's reference count will be one. The size of
46 the memory allocation is determined from the :attr:`tp_basicsize` field of
50 .. cfunction:: TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
52 Allocate a new Python object using the C structure type *TYPE* and the
53 Python type object *type*. Fields not defined by the Python object header
54 are not initialized. The allocated memory allows for the *TYPE* structure
55 plus *size* fields of the size given by the :attr:`tp_itemsize` field of
56 *type*. This is useful for implementing objects like tuples, which are
57 able to determine their size at construction time. Embedding the array of
58 fields into the same allocation decreases the number of allocations,
59 improving the memory management efficiency.
61 .. versionchanged:: 2.5
62 This function used an :ctype:`int` type for *size*. This might require
63 changes in your code for properly supporting 64-bit systems.
66 .. cfunction:: void PyObject_Del(PyObject *op)
68 Releases memory allocated to an object using :cfunc:`PyObject_New` or
69 :cfunc:`PyObject_NewVar`. This is normally called from the
70 :attr:`tp_dealloc` handler specified in the object's type. The fields of
71 the object should not be accessed after this call as the memory is no
72 longer a valid Python object.
75 .. cfunction:: PyObject* Py_InitModule(char *name, PyMethodDef *methods)
77 Create a new module object based on a name and table of functions,
78 returning the new module object.
80 .. versionchanged:: 2.3
81 Older versions of Python did not support *NULL* as the value for the
85 .. cfunction:: PyObject* Py_InitModule3(char *name, PyMethodDef *methods, char *doc)
87 Create a new module object based on a name and table of functions,
88 returning the new module object. If *doc* is non-*NULL*, it will be used
89 to define the docstring for the module.
91 .. versionchanged:: 2.3
92 Older versions of Python did not support *NULL* as the value for the
96 .. cfunction:: PyObject* Py_InitModule4(char *name, PyMethodDef *methods, char *doc, PyObject *self, int apiver)
98 Create a new module object based on a name and table of functions,
99 returning the new module object. If *doc* is non-*NULL*, it will be used
100 to define the docstring for the module. If *self* is non-*NULL*, it will
101 passed to the functions of the module as their (otherwise *NULL*) first
102 parameter. (This was added as an experimental feature, and there are no
103 known uses in the current version of Python.) For *apiver*, the only value
104 which should be passed is defined by the constant
105 :const:`PYTHON_API_VERSION`.
109 Most uses of this function should probably be using the
110 :cfunc:`Py_InitModule3` instead; only use this if you are sure you need
113 .. versionchanged:: 2.3
114 Older versions of Python did not support *NULL* as the value for the
118 .. cvar:: PyObject _Py_NoneStruct
120 Object which is visible in Python as ``None``. This should only be
121 accessed using the ``Py_None`` macro, which evaluates to a pointer to this