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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:41:45+00:00 2026-06-14T02:41:45+00:00

I’ve been reading about magic methods in python, and I’ve found a lot of

  • 0

I’ve been reading about magic methods in python, and I’ve found a lot of info about overriding them and what purpose they serve, but I haven’t been able to find where in the language specific operators and actions are mapped to those methods (+ looks for __add__, += looks for __iadd__, creating a new object from a class might call __new__ and __init__, etc.) Is there somewhere I can see what happens when the python interpreter (or whatever lower level mechanism) encounters a plus sign?

  • 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-14T02:41:48+00:00Added an answer on June 14, 2026 at 2:41 am

    Your question is a bit generic. There is a comprehensive list of “special methods”, even though it misses some stdlib specific methods(e.g. __setstate__ and __getstate__ used by pickle etc. But it’s a protocol of the module pickle not a language protocol).

    If you want to know exactly what the interpreter does you can use the dis module to disassemble the bytecode:

    >>> import dis
    >>> def my_func(a):
    ...     return a + 2
    ... 
    >>> dis.dis(my_func)
      2           0 LOAD_FAST                0 (a)
                  3 LOAD_CONST               1 (2)
                  6 BINARY_ADD          
                  7 RETURN_VALUE   
    

    You can see that the intereper executes a BINARY_ADD byte code when doing addition.
    If you want to see exactly the operations that BINARY_ADD does you can download Python’s source code and check the ceval.c file:

        case BINARY_ADD:
            w = POP();
            v = TOP();
            if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
                /* INLINE: int + int */
                register long a, b, i;
                a = PyInt_AS_LONG(v);
                b = PyInt_AS_LONG(w);
                /* cast to avoid undefined behaviour
                   on overflow */
                i = (long)((unsigned long)a + b);
                if ((i^a) < 0 && (i^b) < 0)
                    goto slow_add;
                x = PyInt_FromLong(i);
            }
            else if (PyString_CheckExact(v) &&
                     PyString_CheckExact(w)) {
                x = string_concatenate(v, w, f, next_instr);
                /* string_concatenate consumed the ref to v */
                goto skip_decref_vx;
            }
            else {
              slow_add:
                x = PyNumber_Add(v, w);
            }
            Py_DECREF(v);
          skip_decref_vx:
            Py_DECREF(w);
            SET_TOP(x);
            if (x != NULL) continue;
            break;
    

    So here we can see that python special cases int and string additions, and eventually falls back to PyNumber_Add, which checks if the first operand implements __add__ and calls it, eventually it tries __radd__ of the right hand side and if nothing works raises a TypeError.

    Note that the byte codes are version-specific, so dis will show different results on different versions:

    # python2.7
    >>> def my_func():
    ...     return map((lambda x: x+1), range(5))
    ... 
    >>> dis.dis(my_func)
      2           0 LOAD_GLOBAL              0 (map)
                  3 LOAD_CONST               1 (<code object <lambda> at 0x16f8c30, file "<stdin>", line 2>)
                  6 MAKE_FUNCTION            0
                  9 LOAD_GLOBAL              1 (range)
                 12 LOAD_CONST               2 (5)
                 15 CALL_FUNCTION            1
                 18 CALL_FUNCTION            2
                 21 RETURN_VALUE        
    # python3
    >>> dis.dis(my_func)
      2           0 LOAD_GLOBAL              0 (map) 
                  3 LOAD_CONST               1 (<code object <lambda> at 0x7f1161a76930, file "<stdin>", line 2>) 
                  6 LOAD_CONST               2 ('my_func.<locals>.<lambda>') 
                  9 MAKE_FUNCTION            0 
                 12 LOAD_GLOBAL              1 (range) 
                 15 LOAD_CONST               3 (5) 
                 18 CALL_FUNCTION            1 (1 positional, 0 keyword pair) 
                 21 CALL_FUNCTION            2 (2 positional, 0 keyword pair) 
                 24 RETURN_VALUE  
    

    Also the same byte code may be optimized in future versions, so even if the byte code is the same different versions of python will actually perform different instructions.

    If you’re interested in learning how python works behind the scenes I’d advise you to write some C extensions, following the tutorials and documentation that you can find on the official python’s website.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I am using Paperclip to handle profile photo uploads in my app. They upload
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.