2

I am developing a new Python data type using type objects. I used the "Defining New Types page for Python 2.7.12" as a guide and was able to adapt the examples on that page to my specific needs. I am able to create my new data type and add it to a Python module. When I run things from Python, I can create a new object like this.

x = MyNewType()

In this case, my tp_new function is called and I am able to fill in the default data items the way I want. However, if I try to do the same thing in C++ by calling PyObject_New(), my tp_new function doesn't get called. As a workaround I can call PyObject_SetAttrString() on each data member with no problem. I see the same issue with my tp_init function, where calling PyObject_Init() does not result in my tp_init function getting called. I've spent the better part of a day trying to track this thing down with no luck. Any help would be appreciated.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Mike Collins
  • 402
  • 3
  • 13

1 Answers1

2

The best way is to call the class object, just like in Python itself.

Credit: I just learnt it from this answer.

Community
  • 1
  • 1
gog
  • 1,220
  • 11
  • 30