Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6125405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:14:37+00:00 2026-05-23T16:14:37+00:00

I am embedding Python in a C++ library which I am making. I would

  • 0

I am embedding Python in a C++ library which I am making. I would like users to be able to pass C functions in the form of function pointers PyObject* (fpFunc*)(PyObject*,PyObject*); so that I can use those functions in the embedded Python.

So I have a function pointer and I know that it is possible to put this function as a module’s method using a PyMethodDef struct and passing it to Py_InitModule("module", ModMethods); and thus obtaining a PyObject* module which I can easily grab functions from.

But, what I would really appreciate is to be able to create this function on the fly without necessarily recreating another module each time.

I have looked into the Python docs, and some of the Python headers to think about a hacky way of doing this without real success… but I’m wondering if’s there’s a more conventional way of doing this.

From what I understood, every function belongs to a module, even __builtin__, so I guess I would require at least on module.

Any idea on how to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:14:37+00:00Added an answer on May 23, 2026 at 4:14 pm

    Found it. Though it’s not in the docs and it’s hardly explicit in the source.

    PyObject* (*fpFunc)(PyObject*,PyObject*) = someFunction;
    PyMethodDef methd = {"methd",fpFunc,METH_VARARGS,"A new function"};
    PyObject* name = PyString_FromString(methd.ml_name);
    PyObject* pyfoo = PyCFunction_NewEx(&methd,NULL,name);
    Py_DECREF(name);
    

    It works. I can call the function like I normally would call a Python function object.

    If you’re curious, all I found in the doc was about Py_InitModule4 and loading modules, so I went to check Python’s source, and found out about PyCFunction, then I looked around in the doc but I couldn’t find anything about PyCFunction_NewEx, so I had to check the source to make sure it was as I thought.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm embedding a Flash video into an HTML and would like the user to
I am embedding a c++ library (binding done with SIP) in my python application.
There are too many method for embedding flash in html, which way is the
I am trying to change the matplotlib font to helvetica, which I'd like to
I like to use python for almost everything and always had clear in my
i am embedding a javascript file inside an svg file like this: <svg xmlns:dc=http://purl.org/dc/elements/1.1/
The problem: I am embedding a CSS file into a custom control library with
I'm working on embedding Python 2.6 into an existing c++ application. So far I
I am embedding Python into my C++ program, and have used PyRun_SimpleString quite effectively
I am embedding python in C/C++ program. What I am trying to do is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.