Python/capi:创建“\uu all\uuuj”对象

2024-09-27 20:18:37 发布

您现在位置:Python中文网/ 问答频道 /正文

在使用普通Python3创建Python模块/库时,我可以轻松地创建__all__列表。然后,在导入模块之后,我可以通过“MODULE.__all__”访问它。你知道吗

但是,在通过Python/capi创建模块时,我无法找到一种简单或“适当”的方法来生成__all__。我查阅了官方文件,没有找到答案。你知道吗

在使用Python/capi制作的模块中创建__all__是否有一种简单的方法或标准方法?你知道吗

我可以使用下面的代码创建类似的对象(比如__author__)。你知道吗

static const char __author__[32] = "Devyn Collier Johnson";
static const char __copyright__[8] = "LGPLv3";
static const char __version__[16] = "2015.11.21";
PyModule_AddStringConstant(m, "__author__", __author__);
PyModule_AddStringConstant(m, "__copyright__", __copyright__);
PyModule_AddStringConstant(m, "__version__", __version__);

下面是我的一个特定模块的代码(为了便于阅读,我删除了函数和大块注释)

#include <Python.h>

#include "geometry.h"


static const char __author__[32] = "Devyn Collier Johnson";

static const char __copyright__[8] = "LGPLv3";

static const char __version__[16] = "2015.11.21";

static const char __all__[16][32] = {
    // Distance
    "distance",
    "linelength",
    // Area
    "areacircle",
    "areasquare",
    "areasquare_int",
    "areasquare_shape"
};


// Docstrings

static char module_docstring[32] =
    "Geometry Equations";
PyDoc_STRVAR(geometry_distance_docstring,
    "distance(location1: tuple, location2: tuple) -> float\nReturn the distance between two points");
PyDoc_STRVAR(geometry_linelength_docstring,
    "linelength(location1: tuple, location2: tuple) -> float\nReturn the length of a line (given two points as tuples with two floats each)");
PyDoc_STRVAR(geometry_areacircle_docstring,
    "areacircle(radius: float) -> float\nReturn the area of a circle (float)");
PyDoc_STRVAR(geometry_areasquare_docstring,
    "areasquare(length: float) -> float\nReturn the area of a square (float)");
PyDoc_STRVAR(geometry_areasquare_int_docstring,
    "areasquare_int(length: int) -> int\nReturn the area of a square (int)");
PyDoc_STRVAR(geometry_areasquare_shape_docstring,
    "areasquare_shape(square: float) -> float\nReturn the area of a square (float)");


// Function Definitions

static PyObject *geometry_distance(PyObject *self, PyObject *args);
static PyObject *geometry_areacircle(PyObject *self, PyObject *args);
static PyObject *geometry_areasquare(PyObject *self, PyObject *args);
static PyObject *geometry_areasquare_int(PyObject *self, PyObject *args);
static PyObject *geometry_areasquare_shape(PyObject *self, PyObject *args);


// Module Specification

static PyMethodDef module_methods[] = {
    {"distance", (PyCFunction) geometry_distance, METH_VARARGS, geometry_distance_docstring},
    {"linelength", (PyCFunction) geometry_distance, METH_VARARGS, geometry_linelength_docstring},
    {"areacircle", (PyCFunction) geometry_areacircle, METH_VARARGS, geometry_areacircle_docstring},
    {"areasquare", (PyCFunction) geometry_areasquare, METH_VARARGS, geometry_areasquare_docstring},
    {"areasquare_int", (PyCFunction) geometry_areasquare_int, METH_VARARGS, geometry_areasquare_int_docstring},
    {"areasquare_shape", (PyCFunction) geometry_areasquare_shape, METH_VARARGS, geometry_areasquare_shape_docstring},
    {NULL, NULL, 0, NULL}
};


static struct PyModuleDef module = {
    PyModuleDef_HEAD_INIT,
    "geometry",
    module_docstring,
    -1,
    module_methods
};


PyMODINIT_FUNC PyInit_geometry(void) {
    PyObject *m;
    m = PyModule_Create(&module);
    PyModule_AddStringConstant(m, "__author__", __author__);
    PyModule_AddStringConstant(m, "__copyright__", __copyright__);
    PyModule_AddStringConstant(m, "__version__", __version__);
    if (m == NULL)
        return NULL;
    return m;
}

Tags: staticfloatauthorintdistancedocstringpyobjectcopyright

热门问题