pybind / pybind/pybind11

Embedding python cannot return the python derived class

Open
#1,971 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Hi, I followed the example of embedding the Widget Class in python:
https://github.com/pybind/pybind11/blob/master/tests/test_embed/test_interpreter.cpp

I am using the same code inside a class that I created. I am trying to make the python class derivedWidget a class member or be able to return it and pass it between class members.

I am getting the error that I cannot allocate an object of abstract type.

How can py_widget or cpp_widget be a returned type?
How can py_widget or cpp_widget be declared a global variable?
How can py_widget or cpp_widget be declared a variable of a new class?

py::module py_widget = module.attr("DerivedWidget")("The question");
auto message = py_widget.attr("the_message");
const Widget &cpp_widget = py_widget.cast<const Widget &>();

Thank you.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with tests/test_embed/test_interpreter.cpp, the embedding example referenced in the issue, and compare it with the class-member use case described here. Determine whether the reported abstract-type error is an unsupported usage or a pybind11 defect; done requires a confirmed supported approach or a narrowly reproducible issue suitable for a separate fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
developer-experience
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.