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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:59:53+00:00 2026-06-13T18:59:53+00:00

I have a problem with Cython usage of default constructors. My C++ class Node

  • 0

I have a problem with Cython usage of default constructors.

My C++ class Node is the following

Node.h

class Node
{
public:
    Node()
    { 
        std::cerr << "calling no arg constructor" << std::endl;
            w=0.0;
        d=0.0;
    }
    Node(double val, double val2);
    {
            std::cerr << "calling 2 args constructor" << std::endl;
        this->w=val;
        this->d=val2;
    }
private:
    double d,w;
}

is wrapped in Cython as follows

cdef extern from "Node.h":
    cdef cppclass Node:
        Node() except +
        Node(double val1, double val2) except +
        double d
        double w

cdef class pyNode:
    cdef Node *thisptr      # hold a C++ instance which we're wrapping

    def __cinit__(self):
        self.thisptr = new Node()

    def __cinit__(self, double val1, double val2):
        self.thisptr = new Node(val1,val2)

    def __dealloc__(self):
        del self.thisptr

    def __repr__(self):
        return "d=%s w=%s" % (self.thisptr.w, self.thisptr.w )

The Cython code compiles well, but in particular when called from Python

from pyNode import pyNode as Node
n=Node(1.0,2.0)

I get the expected calling 2 args constructor string, but if I’m trying to declare a Node object from python using the “no-arguments” constructor (which should be correctly declared as __cinit__(self) I’m getting no output, this means that the no-argument constructor is not called!

How can I explicitly call it from the cinit method of the wrapped class?

  • 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-13T18:59:54+00:00Added an answer on June 13, 2026 at 6:59 pm

    The issue here is that you can’t overload __cinit__() like that (as only cdef functions can be overloaded) – instead, have it take default values, then call the right thing as needed.

    Edit: Essentially, you need to implement the function in a way closer to how you would in normal Python code, instead of using overloading:

    def __cinit__(self, double val1=-1, double val2=-1): 
        if val1 == -1 or val2 == -1:
            self.thisptr = new Node()
        else:
            self.thisptr = new Node(val1,val2)
    

    Naturally, this presumes -1 is a value that isn’t useful for the function, you could use another value, or if you need every value of a double to be valid, then you might need to remove the typing, taking a Python object so that you can use None as the default:

    def __cinit__(self, val1=None, val2=None):
        if val1 is not None and val2 is not None:
            self.thisptr = new Node(val1, val2)
        else:
            self.thisptr = new Node()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with context menu. I have got: @Override public void onCreateContextMenu(ContextMenu menu,
I have problem with consuming DbContext class in EF 4.3 CodeFirst approach. As for
I have problem with css styles in aspx.cs file HttpContext.Current.Response.Write(<table class=\'bordered\' cellpadding=\'5\' color: \'red\'
I have a C++ class with a virtual method: //C++ class A { public:
I have problem to find a good namespace design and class order. Imagine we
I have problem starting an intent that extends Fragment , from a class that
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have problem with show or hide form in Window Form Application. I start

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.