Giter Site home page Giter Site logo

Comments (9)

wjakob avatar wjakob commented on August 19, 2024

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.

mlund avatar mlund commented on August 19, 2024

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.

wjakob avatar wjakob commented on August 19, 2024

Can't you just avoid including <pybind11/stl.h>? In that case, your custom type definition will have precedence.

from pybind11.

wjakob avatar wjakob commented on August 19, 2024

(other than that, your class definition looks good)

from pybind11.

mlund avatar mlund commented on August 19, 2024

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.

wjakob avatar wjakob commented on August 19, 2024

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.

mlund avatar mlund commented on August 19, 2024

I can surely imagine it's non-trivial; I'll post my solution/workaround here when done. Thanks / Mikael

from pybind11.

levinandrew avatar levinandrew commented on August 19, 2024

@mlund were you able to find a solution to this issue?

from pybind11.

vagrawal-aptina avatar vagrawal-aptina commented on August 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.