pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.77k stars 2.11k forks source link

Embedding python in c++: passing a python class to a python function call in another python class #1916

Closed rachelglenn closed 5 years ago

rachelglenn commented 5 years ago

/////Dog.py class Dog(object): def init(self,breed_in=""): self.breed = breed_in def get_breed(self): return self.breed

////Groomer.py from Dog import Dog class Groomer: def init(self, newDogIn ): self.NewDog = Dog(newDogIn.breed)

/////////main.cpp

include <pybind11/pybind11.h>

include <pybind11/embed.h>

include

int main() { pybind11::scoped_interpreter guard{};

std::string breed = "Healer";

//Create the python class pybind11::object new_dog = pybind11::module::import("Dog");

new_dog.attr("Dog")(pybind11.cast(breed)); //Not sure if I am doing this step correctly

//Check I am creating the instance correctly std::cout << "New Dog is: <<( new_dog.attr("Dog).attr("get_breed") ).cast()<< std::endl; // Error #1 This step doesn't work no attribute get_breed

// Create the python class to take python class dog pybind11::object GroomerObject = pybind11::module::import("Groomer");

GroomerObject.attr("Groomer")(new_dog);

return 0;

}

//Error #2 : class Dog has no attribute breed

rachelglenn commented 5 years ago

I found the issue. It was with how the functions were called.

orlandini commented 3 years ago

What exactly was the issue?

xkungfu commented 3 years ago

where is the issue?