Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rachelglenn opened this issue Sep 9, 2019 · 3 comments

Comments

@rachelglenn
Copy link

rachelglenn commented Sep 9, 2019

/////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") ).caststd::string()<< 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
Copy link
Author

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

@orlandini
Copy link

What exactly was the issue?

@xkungfu
Copy link

xkungfu commented Oct 17, 2021

where is the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants