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

  • SEARCH
  • Home
  • 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 135959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:52:22+00:00 2026-05-11T06:52:22+00:00

A friend and I have been toying around with various Python C++ wrappers lately,

  • 0

A friend and I have been toying around with various Python C++ wrappers lately, trying to find one that meets the needs of both some professional and hobby projects. We’ve both honed in on PyCxx as a good balance between being lightweight and easy to interface with while hiding away some of the ugliest bits of the Python C api. PyCxx is not terribly robust when it comes to exposing types, however (ie: it instructs you to create type factories rather than implement constructors), and we have been working on filling in the gaps in order to expose our types in a more functional manner. In order to fill these gaps we turn to the C api.

This has left us with some questions, however, that the api documentation doesn’t seem to cover in much depth (and when it does, the answers are occasionally contradictory). The basic overarching question is simply this: What must be defined for a Python type to function as a base type? We’ve found that for the PyCxx class to function as a type we need to define tp_new and tp_dealloc explicitly and set the type as a module attribute, and that we need to have Py_TPFLAGS_BASETYPE set on [our type]->tp_flags, but beyond that we’re still groping in the dark.

Here is our code thus far:

class kitty : public Py::PythonExtension<kitty> { public:     kitty() : Py::PythonExtension<kitty>() {}     virtual ~kitty() {}      static void init_type() {         behaviors().name('kitty');         add_varargs_method('speak', &kitty::speak);     }      static PyObject* tp_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) {         return static_cast<PyObject*>(new kitty());     }      static void tp_dealloc(PyObject *obj) {         kitty* k = static_cast<kitty*>(obj);         delete k;     }  private:     Py::Object speak(const Py::Tuple &args) {         cout << 'Meow!' << endl;         return Py::None();     } };  // cat Module class cat_module : public Py::ExtensionModule<cat_module> { public:      cat_module() : Py::ExtensionModule<cat_module>('cat') {          kitty::init_type();          // Set up additional properties on the kitty type object         PyTypeObject* kittyType = kitty::type_object();         kittyType->tp_new = &kitty::tp_new;         kittyType->tp_dealloc = &kitty::tp_dealloc;         kittyType->tp_flags |= Py_TPFLAGS_BASETYPE;          // Expose the kitty type through the module         module().setAttr('kitty', Py::Object((PyObject*)kittyType));          initialize();     }     virtual ~cat_module() {} };  extern 'C' void initcat() {     static cat_module* cat = new cat_module(); } 

And our Python test code looks like this:

import cat  class meanKitty(cat.kitty):     def scratch(self):         print 'hiss! *scratch*'  myKitty = cat.kitty() myKitty.speak()  meanKitty = meanKitty() meanKitty.speak() meanKitty.scratch() 

The curious bit is that if you comment all the meanKitty bits out, the script runs and the cat meows just fine, but if you uncomment the meanKitty class suddenly Python gives us this:

AttributeError: 'kitty' object has no attribute 'speak' 

Which confuses the crap out of me. It’s as if inheriting from it hides the base class entirely! If anyone could provide some insight into what we are missing, it would be appreciated! Thanks!


EDIT: Okay, so about five seconds after posting this I recalled something that we had wanted to try earlier. I added the following code to kitty –

virtual Py::Object getattr( const char *name ) {     return getattr_methods( name ); } 

And now we’re meowing on both kitties in Python! still not fully there, however, because now I get this:

Traceback (most recent call last):     File 'd:\Development\Junk Projects\PythonCxx\Toji.py', line 12, in <module>     meanKitty.scratch() AttributeError: scratch 

So still looking for some help! Thanks!

  • 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. 2026-05-11T06:52:23+00:00Added an answer on May 11, 2026 at 6:52 am

    You must declare kitty as class new_style_class: public Py::PythonClass< new_style_class >. See simple.cxx and the Python test case at http://cxx.svn.sourceforge.net/viewvc/cxx/trunk/CXX/Demo/Python3/.

    Python 2.2 introduced new-style classes which among other things allow the user to subclass built-in types (like your new built-in type). Inheritance didn’t work in your example because it defines an old-style class.

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

Sidebar

Ask A Question

Stats

  • Questions 121k
  • Answers 121k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There must be some better than one I wrote recently,… May 12, 2026 at 12:19 am
  • Editorial Team
    Editorial Team added an answer $current = $root; foreach($row as $fieldname => $fieldvalue) { $next… May 12, 2026 at 12:19 am
  • Editorial Team
    Editorial Team added an answer Numerous sample of connection strings here : http://www.connectionstrings.com/ May 12, 2026 at 12:19 am

Related Questions

I've been playing around with some things and thought up the idea of trying
I've just found out about Stack Overflow and I'm just checking if there are
I am attempting to extract all instances of a particular format from a string:
What are the advantages/disadvantages of keeping SQL in your C# source code or in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.