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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:22:30+00:00 2026-06-18T14:22:30+00:00

The C PyObject structure contains the fields tp_as_number , tp_as_sequence and tp_as_mapping . In

  • 0

The C PyObject structure contains the fields tp_as_number, tp_as_sequence and tp_as_mapping. In which circumstances are these invoked? Can anybody provide some example Python code which would result in these C methods being called?

  • 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-18T14:22:31+00:00Added an answer on June 18, 2026 at 2:22 pm

    Those methods are the equivalent of python’s special methods, and are called in the same circumstances. For example tp_as_number->nb_add is called when executing a + b and a is the extension type.
    It is the equivalent of __add__. The inplace_* functions are the equivalents of __i*__ methods.

    Note that the __r*__ methods are implemented simply swapping arguments to the normal functions, thus 5 + a where a is an extension type will first try to call the numeric version of nb_add, after this failed it tries nb_add of a putting 5 as first argument and a as the second one.

    The same is true for the tp_as_mapping and tp_as_sequence structs. The mp_length and sq_length functions are called by the built-in function len, and are the equivalent of __len__. Theoretically you could implement different functions for mp_length and sq_length, in which case the sq_length has precedence(this can be seen from the source code, even though I don’t know whether this behaviour is documented).

    Also note that, for example, the + operator can be implemented in different functions. The sq_concat is called after trying nb_add, and thus an extension type can support + operator without having an nb_add function set.

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

Sidebar

Related Questions

How can I get a char* from a PyObject which points to a string.
I get some compile time errors and I can't understand why that is. The
I've got some code which compiles a C function to create a numpy generic
I am lost on Py_DECREF/INCREF when handling PyList_Append. Can anybody have comments on the
When I have a PyObject * obtained from PyArg_ParseTuple , do I need to
I have a dll with a function that takes PyObject as argument something like
Is there anyway to discover the python value of a PyObject* from a corefile
How can I get a transparent cursor using Python GTK3 and pyGObject. I have
I have this code in a C application that's embedding Python (2.7.1): { PyObject
I am writing a native function that will return multiple Python objects PyObject *V

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.