PyMethodDef
from Python.h
allows to specify Cpp-built functions to use in Python
. However, there is much doubt if this can be applied for Cpp-built classes and I can't find anything like PyClassDef
which could presumably help me to do so. All I've managed to find concerns class methods, but not the class itself. Is it possible to build a class in C++
with regular Python-C-API
to use as a regular class in Python
?
Asked
Active
Viewed 499 times
0

Kaiyakha
- 1,463
- 1
- 6
- 19
1 Answers
2
You can use pybind11 to implement python call c++ native class. Link here pybind11 class it's easier use than cpython.

jebin
- 36
- 3
-
That's a option indeed, but is there a way to implement it using standard `python-c-api`? I just haven't got familiar with `pybind11` yet :) If no, well I should learn it – Kaiyakha Nov 19 '21 at 12:10
-
1@Kaiyakha IMHO it is much easier to use something like `Boost.Python`, `Cython` or `pybind11` instead of dealing with everything these tools take care of for you ... – fxweidinger Nov 19 '21 at 12:14
-
@fxweidinger since I have just started learning `python-c-api`, it's reasonable to mess around with basic things to better understand how it works, isn't it? – Kaiyakha Nov 19 '21 at 12:16
-
@Kaiyakha it obviously depends on your use case and the work you can put into it. This [tutorial](https://intermediate-and-advanced-software-carpentry.readthedocs.io/en/latest/c++-wrapping.html#manual-wrapping) might be of use to you. – fxweidinger Nov 19 '21 at 12:17
-
@fxweidinger OK it's clear there are better means. But back to the muttons, can I build a class using specifically regular `python-c-api`? – Kaiyakha Nov 19 '21 at 12:22
-
@Kaiyakha I think if you want to write fairly regular C++ classes and syntax Boost.Python is the way to go. If you're willing to deal with nasty syntax and `PyObject` look at [this](https://bertvandenbroucke.netlify.app/2019/11/20/exposing-c++-to-python-an-introduction/) , scroll down to heading "Python C API" – fxweidinger Nov 19 '21 at 12:35
-
@fxweidinger well there is nothing about classes in regular `python-c-api` though – Kaiyakha Nov 19 '21 at 12:41
-
3pybind11 is wrapper of python-c-api, you can learn it from pybind11 source code if you need. CPython haven't a c++ class wrapper, so you need implement a python class to wrap c++ class use native CPython. – jebin Nov 19 '21 at 12:52
-
@cepin _CPython haven't a c++ class wrapper, so you need implement a python class to wrap c++ class use native CPython._ Now we're talking, that's the answer I've been waining for! You can add this to your answer and I'll accept it. Someone else may be wondering about the issue later – Kaiyakha Nov 19 '21 at 13:02
-
@Kaiyakha Ah well, I think the wording of your question was a little misleading to me. But good to know, you found what you were looking for. – fxweidinger Nov 19 '21 at 13:30
-
@fxweidinger I guess I should've specified the way I wanted to wrap a cpp class. Added that to the question already – Kaiyakha Nov 19 '21 at 14:23