I am able to use SWIG_NewPointerObj to pass a C++ object to Python, but it’s not the PyObject* so I can’t use it properly in Python.
My question is: Is there a way to fetch the PyObject* of a C++ object that was created from Python, in C++?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There isn’t a unique
PyObjectper C++ object. For example if you wrap the following:Every call to
test()inside Python will return a newPyObject– SWIG has no easy way to know they’re identical.The
PyObjects that get returned are thin wrappers, i.e. proxies around the C++ object that have no local state. From the Python perspective these all look and behave like the same object because they delegate everything to the C++ object.If you want to get hold of a
PyObjectthere are ways you can do this, but in general it’s not a good idea because any changes you make to that object won’t be visible from otherPyObjects even if they refer to the same C++ object.