How to detect method calls made by Python behind the scenes?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite
2












In particular, I want to see which magic method is being called by a particular line of code.



For instance, I know that 1 + 2 actually calls (1).__add__(2) and [1,2,3][0] calls [1,2,3].__getitem__(0).



I'd like to know which magic methods are called for other operations without having to look it up online.







share|improve this question

















  • 1




    use the dis module to get the opcode for the operation and then you can dig through ceval.c in the source code, which has a big switch on opcodes, to see where that takes you, but this can be non-trivial, depending on the operation
    – C S
    Aug 6 at 6:38














up vote
7
down vote

favorite
2












In particular, I want to see which magic method is being called by a particular line of code.



For instance, I know that 1 + 2 actually calls (1).__add__(2) and [1,2,3][0] calls [1,2,3].__getitem__(0).



I'd like to know which magic methods are called for other operations without having to look it up online.







share|improve this question

















  • 1




    use the dis module to get the opcode for the operation and then you can dig through ceval.c in the source code, which has a big switch on opcodes, to see where that takes you, but this can be non-trivial, depending on the operation
    – C S
    Aug 6 at 6:38












up vote
7
down vote

favorite
2









up vote
7
down vote

favorite
2






2





In particular, I want to see which magic method is being called by a particular line of code.



For instance, I know that 1 + 2 actually calls (1).__add__(2) and [1,2,3][0] calls [1,2,3].__getitem__(0).



I'd like to know which magic methods are called for other operations without having to look it up online.







share|improve this question













In particular, I want to see which magic method is being called by a particular line of code.



For instance, I know that 1 + 2 actually calls (1).__add__(2) and [1,2,3][0] calls [1,2,3].__getitem__(0).



I'd like to know which magic methods are called for other operations without having to look it up online.









share|improve this question












share|improve this question




share|improve this question








edited Aug 6 at 6:29









Ry-♦

161k34325345




161k34325345









asked Aug 6 at 6:23









japseow

536




536







  • 1




    use the dis module to get the opcode for the operation and then you can dig through ceval.c in the source code, which has a big switch on opcodes, to see where that takes you, but this can be non-trivial, depending on the operation
    – C S
    Aug 6 at 6:38












  • 1




    use the dis module to get the opcode for the operation and then you can dig through ceval.c in the source code, which has a big switch on opcodes, to see where that takes you, but this can be non-trivial, depending on the operation
    – C S
    Aug 6 at 6:38







1




1




use the dis module to get the opcode for the operation and then you can dig through ceval.c in the source code, which has a big switch on opcodes, to see where that takes you, but this can be non-trivial, depending on the operation
– C S
Aug 6 at 6:38




use the dis module to get the opcode for the operation and then you can dig through ceval.c in the source code, which has a big switch on opcodes, to see where that takes you, but this can be non-trivial, depending on the operation
– C S
Aug 6 at 6:38










1 Answer
1






active

oldest

votes

















up vote
8
down vote













There isn't a good way to inspect that. You should probably just look it up.



In the implementation (specifically for CPython), 1 + 2 or [1, 2, 3][0] won't actually go through the __add__ or __getitem__ methods at all; they'll go through C-level hooks and skip the methods entirely. Even if they went through the methods, it'd all happen in C-level code, which you can't debug with PDB or do much of anything to inspect.



The closest I have to something matching the spirit of what you're looking for is



>>> import unittest.mock
>>> unittest.mock.MagicMock() + 3
<MagicMock name='mock.__add__()' id='140290799397408'>


so hey, look! + uses __add__. That's something you can run to see what magic method is invoked for +. It doesn't involve actually inspecting the magic methods involved in +, though. MagicMock just has already-written implementations of most of the standard magic methods.






share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51701643%2fhow-to-detect-method-calls-made-by-python-behind-the-scenes%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    8
    down vote













    There isn't a good way to inspect that. You should probably just look it up.



    In the implementation (specifically for CPython), 1 + 2 or [1, 2, 3][0] won't actually go through the __add__ or __getitem__ methods at all; they'll go through C-level hooks and skip the methods entirely. Even if they went through the methods, it'd all happen in C-level code, which you can't debug with PDB or do much of anything to inspect.



    The closest I have to something matching the spirit of what you're looking for is



    >>> import unittest.mock
    >>> unittest.mock.MagicMock() + 3
    <MagicMock name='mock.__add__()' id='140290799397408'>


    so hey, look! + uses __add__. That's something you can run to see what magic method is invoked for +. It doesn't involve actually inspecting the magic methods involved in +, though. MagicMock just has already-written implementations of most of the standard magic methods.






    share|improve this answer

























      up vote
      8
      down vote













      There isn't a good way to inspect that. You should probably just look it up.



      In the implementation (specifically for CPython), 1 + 2 or [1, 2, 3][0] won't actually go through the __add__ or __getitem__ methods at all; they'll go through C-level hooks and skip the methods entirely. Even if they went through the methods, it'd all happen in C-level code, which you can't debug with PDB or do much of anything to inspect.



      The closest I have to something matching the spirit of what you're looking for is



      >>> import unittest.mock
      >>> unittest.mock.MagicMock() + 3
      <MagicMock name='mock.__add__()' id='140290799397408'>


      so hey, look! + uses __add__. That's something you can run to see what magic method is invoked for +. It doesn't involve actually inspecting the magic methods involved in +, though. MagicMock just has already-written implementations of most of the standard magic methods.






      share|improve this answer























        up vote
        8
        down vote










        up vote
        8
        down vote









        There isn't a good way to inspect that. You should probably just look it up.



        In the implementation (specifically for CPython), 1 + 2 or [1, 2, 3][0] won't actually go through the __add__ or __getitem__ methods at all; they'll go through C-level hooks and skip the methods entirely. Even if they went through the methods, it'd all happen in C-level code, which you can't debug with PDB or do much of anything to inspect.



        The closest I have to something matching the spirit of what you're looking for is



        >>> import unittest.mock
        >>> unittest.mock.MagicMock() + 3
        <MagicMock name='mock.__add__()' id='140290799397408'>


        so hey, look! + uses __add__. That's something you can run to see what magic method is invoked for +. It doesn't involve actually inspecting the magic methods involved in +, though. MagicMock just has already-written implementations of most of the standard magic methods.






        share|improve this answer













        There isn't a good way to inspect that. You should probably just look it up.



        In the implementation (specifically for CPython), 1 + 2 or [1, 2, 3][0] won't actually go through the __add__ or __getitem__ methods at all; they'll go through C-level hooks and skip the methods entirely. Even if they went through the methods, it'd all happen in C-level code, which you can't debug with PDB or do much of anything to inspect.



        The closest I have to something matching the spirit of what you're looking for is



        >>> import unittest.mock
        >>> unittest.mock.MagicMock() + 3
        <MagicMock name='mock.__add__()' id='140290799397408'>


        so hey, look! + uses __add__. That's something you can run to see what magic method is invoked for +. It doesn't involve actually inspecting the magic methods involved in +, though. MagicMock just has already-written implementations of most of the standard magic methods.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Aug 6 at 6:39









        user2357112

        137k11141212




        137k11141212






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51701643%2fhow-to-detect-method-calls-made-by-python-behind-the-scenes%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What is the equation of a 3D cone with generalised tilt?

            Color the edges and diagonals of a regular polygon

            Relationship between determinant of matrix and determinant of adjoint?