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 7863945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:38:27+00:00 2026-06-02T23:38:27+00:00

As I understand the relationship between C and C++, the latter is essentially an

  • 0

As I understand the relationship between C and C++, the latter is essentially an extension of the former and retains a certain degree of backwards compatibility. Is it safe to assume that the python C API can be called with C++ code?

More to the point, I notice that the official python documentation bundles C and C++ extensions together on the same page. Nowhere am I able to find a C++ API. This leads me to believe that the same API is safe to use in both languages.

Can someone confirm or deny this?

EDIT:

I think I made my question much more complicated than it needs to be. The question is this: what must I do in order to write a python module in C++? Do I just follow the same directions as listed here, substituting C code for C++? Is there a separate API?

  • 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-06-02T23:38:28+00:00Added an answer on June 2, 2026 at 11:38 pm

    I can confirm that the same Python C API is safe to be used in both languages, C and C++.
    However, it is difficult to provide you with more detailed answer, unless you will ask more specific question. There are numerous caveats and issues you should be aware of. For example, your Python extensions are defined as C types struct, not as C++, so don’t expect to have their constructor/destructor implicitly defined and called.

    For example, taking the sample code from Defining New Types in the Python manual, it can be written in C++ way and you can even blend-in C++ types:

    // noddy.cpp
    namespace {
    
    struct noddy_NoddyObject
    {
        PyObject_HEAD
        // Type-specific fields go here.
        std::shared_ptr<int> value; // WARNING
    };
    
    PyObject* Noddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
    {
        try {
            Noddy *self = (Noddy *)type->tp_alloc(type, 0);
            if (self) {
                self->value = std::make_shared(7);
                // or more complex operations that may throw
                // or extract complex initialisation as Noddy_init function
                return self;
            }
        }
        catch (...) {
            // do something, log, etc.
        }
        return 0;
    }
    
    PyTypeObject noddy_NoddyType =
    {
        PyObject_HEAD_INIT(NULL)
        // ...
    }
    
    } // unnamed namespace
    

    But, neither constructor nor destructor of the std::shared_ptr will be called.
    So, remember to define dealloc function for your noddy_NoddyType where you will reset the value with nullptr. Why even bother with having value defined as shared_ptr, you may ask. It is useful if you use your Python extension in C++, with exceptions, to avoid type conversions and casts, to have more seamless integration inside definitions of your implementation, error handling based on exception may be easier then, etc.
    And in spite of the fact that your objects of the noddy_NoddyType are managed by machinery implemented in pure C, thanks to dealloc function the value will be released according to well-known RAII rules.

    Here you can find interesting example of nearly seamless integration of Python C API with the C++ language: How To catch Python stdout in c++ code

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

Sidebar

Related Questions

I'm trying to understand the relationship between two git commits that are some distance
Folks, I am trying to understand the relationship between video bitrate, image size, codec,
I am trying to understand some things about jump tables and its relationship between
I am trying to understand the relationship between android.graphics package and SurfaceFlinger . Based
I am a Delphi novice, but I'm trying to understand the relationship between TApplication
I would like to understand relationship between jQuery object and DOM element.. When jQuery
Trying to understand the relationship between UIView and CALayer. I read Apple documentation but
When using Mercurial I sometimes find that it is hard to understand the relationship
I want to understand the relationship between a record being created in the users
I am trying to construct an e-commerce database but I don't understand relationship between

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.