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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:08:44+00:00 2026-06-11T12:08:44+00:00

I’m writing a new extension type, but I have a problem setting the numeric

  • 0

I’m writing a new extension type, but I have a problem setting the numeric operations(such as addition/subtraction/multiplication). I have managed to set the some in-place operations, while the normal operations are not called.

For example, I have the function:

static PyObject *
MyType_Mul(PyObject *v, PyObject *w)
{
    PyErr_SetString(PyExc_ValueError, "testing");
    return NULL;
}

And I set it in the numbers methods like this:

static PyNumberMethods my_type_as_number = {
    0,  /* nb_add */
    0,  /* nb_sub */
    (binaryfunc)MyType_Mul,  /* nb_mul */
    ...
    0,  /* nb_in_place_add */
    0,  /* nb_in_place_sub */
    (binaryfunc)MyType_Mul,  /* nb_in_place_mul */
    ...
};

Now when I try to use my type I get this kind of behaviour:

>>> from mytype import MyType
>>> a = MyType()
>>> a * 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'mytype.MyType' and 'int'
>>> 2 * a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'int' and 'mytype.MyType'

But if I use the in-place operator:

>>> a *= 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: testing

If I use dir() on the object I can see the __mul__ and __rmul__ methods(which means that python sees them), but it seems like they are not called at all.
Using a.__mul__(2) returns NotImplemented.

Also:

>>> a.__mul__
<method-wrapper '__mul__' of mytype.MyType object at 0x7fc2ecc50468>
>>> a.__imul__
<method-wrapper '__imul__' of mytype.MyType object at 0x7fc2ecc50468>

so, as you can see, they are exactly the same thing.

What’s going on? Why the same exact function works for the in-place operator but not the “normal” operator? I’ve also thought that I might used the wrong slot, but I double checked and it is correct, and also setting it to nb_add, nb_sub etc. does not work.

  • 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-11T12:08:45+00:00Added an answer on June 11, 2026 at 12:08 pm

    Thanks to nneonneo’s comments I understood what was wrong. Basically I forgot to set the Py_TPFLAGS_CHECKTYPES flag.

    There are some clues about this absence in the description I gave:

    1. The methods are the same object, but it acts differently for in-place operations
    2. In the comments I also said that, for example, doing a*a would yield the correct result, while doing a*different-type does not.

    This clearly means that the interpreter, when doing non-in-place operations, is checking the types of the arguments and calls my function if the type is MyType, otherwise returns NotImplemented.

    Searching a bit in the documentation it’s quite easy to see that this is the default behaviour for numeric methods.

    If the argument type is not of the same class, then it is assumed that the operation is not implemented.

    To allow different types to be “operated” together you have to set the Py_TPFLAGS_CHECKTYPES flag in the MyType:

    static PyTypeObject MyType = {
        PyObject_HEAD_INIT(&PyType_Type)
        0,                         /*ob_size*/
        "mytype.MyType",           /*tp_name*/
        sizeof(MyTypeObject),      /*tp_basicsize*/
        0,                         /*tp_itemsize*/
        ...
        0,                         /*tp_repr*/
        &mytype_as_number,         /*tp_as_number*/
        0,                         /*tp_as_sequence*/
        ...
        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES,/*tp_flags*/
        ...
    };
    

    With this flag set the interpreter wont check the types and so you have to deal with them manually.

    The in-place operators, instead, always allow different types. Why is that, I don’t know.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.