Comments (9)
The STL auto-binding code in stl.h
transparently converts a std::vector to a Python list, but it does not transfer back changes (this is intentional, it would be costly to have to do a full sweep over the vector after each function call to detect changes and re-synchronize).
You will have to create your own bindings for a custom std::vector that "stays" on the C++ side, and which can be accessed from Python using iterators and the slicing protocol. See example 6 of the included demo plugin.
from pybind11.
Thanks. I'd rather not change the c++ side and tried to define my vector as:
py::class_<Tspace::particlevector> _pvec(m, "particlevector");
_pvec.def("__len__", &Tspace::particlevector::size);
hoping that Space.p
would be returned as python type particlevector
but it's still list
. Any way to override the automatic STL conversion other than removing the header which is otherwise needed? I understand the performance issue mentioned in your last post, but wonder if it's possible to copy only for the case where the list is passed as a non-const argument. It certainly seems more intuitive, safe.
from pybind11.
Can't you just avoid including <pybind11/stl.h>
? In that case, your custom type definition will have precedence.
from pybind11.
(other than that, your class definition looks good)
from pybind11.
Sure, but then all stl return types need to be manually specialized to get pythonic behavior, right? The project is rather large so slightly more fine-grained control of the implicit stl conversion would probably be useful, perhaps as an option ala the return policies. Thanks / Mikael
from pybind11.
It's tricky due to do this since the type casting system relies on template partial overloads that can't just be turned off once they have been introduced. I'm all ears if you have an idea, but I hesitate to complicate the library for this use case.
from pybind11.
I can surely imagine it's non-trivial; I'll post my solution/workaround here when done. Thanks / Mikael
from pybind11.
@mlund were you able to find a solution to this issue?
from pybind11.
hi, did you get the solution/workaround for this problem?
I am facing a very similar problem in my code.
Any help will be highly appreciated.
Thanks, Vishal
from pybind11.
Related Issues (20)
- [Bug]: Pybind failed to build due to error LNK2019 on windows with MSVC Target ARM64
- Linking is really slow with Ninja CMake generator, but not Unix Makefiles
- [BUG]: bounds checking problem HOT 6
- [Issue]: Segmentation Fault When Sequentially Calling Two Independent Pybind11 Functions
- [BUG]: pybind11 failed to build due to ImportError: DLL load failed while importing test_cmake_build on windows with MSVC ASAN
- [BUG]: iterators are needlessly slow / need an exception-free way of finishing. HOT 1
- [BUG]: 6 tests fail
- [BUG]: Strict aliasing error on MinGW
- [BUG]: gil_safe_call_once_and_store is not NoGil thread safe. HOT 1
- [BUG]: CVE-2022-42969 being flagged in project using pybind
- [BUG]: `FindPythonLibsNew.cmake` failed to find Python libraries with Python 3.13t on Windows HOT 1
- [BUG]: Variant value reset to default when passing Python instance to C++ HOT 1
- [BUG]: static numpy array_t leads to segfault on exit on Python 3.12
- [BUG]: implicitly_convertible with templated constructor
- [BUG]: Program crashes if std::lock_guard or std::unique_lock used on windows. HOT 1
- [BUG]: NumPy ImportError with Embedded Interpreter in Adobe After Effects Plugin Using pybind11 HOT 1
- [BUG]: Eigen::SparseMatrix -> unexpected overload-resolution / template-instantiation
- [BUG]: Can't catch the exception in Python that raise in C++ using py::error_already_set()
- [BUG]: pybind11-config --includes does not quote paths HOT 8
- [BUG]: token pasting of ',' and __VA_ARGS__ is a GNU extension / pedantic warnings HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pybind11.