Access Python attributes at construction time, C++ side #4114
adriendelsalle
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description
Hi! Thanks for this great project!
I'm trying to use a trampoline class to add/modify Python attributes at construction time, C++ side.
This is a pretty handy feature to be able to add dynamically or init some attributes at construction time.
Reading the docs I was expecting this to be already the case:
I didn't managed to make it work from master. Here is a minimal example to explain the intent:
CMakeLists.txt
filemkdir build cd build/ cmake .. make install
Running the test script
python test.py
gives anAttributeError
:As far as I understand, the
py::cast(this)
doesn't behave correctly because the C++ object is not yet allocated and registered.To make it possible (correct me if I'm wrong..), the following changes would be necessary:
value_ptr
I saw that placement new op was removed earlier to simplify the syntax when using a custom
new
operator (see #948 and #805).Maybe a good option is to keep the current behavior as is and pass an extra option (
py::preallocate
) for people willing to do the described behavior (in that case it would probably need to add placement new operator if needed)?I started some tries, and would be happy to open a PR to discuss on changes!
Beta Was this translation helpful? Give feedback.
All reactions