How to detect method calls made by Python behind the scenes?
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
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.
python
add a comment |Â
up vote
7
down vote
favorite
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.
python
1
use thedis
module to get the opcode for the operation and then you can dig throughceval.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
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
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.
python
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.
python
edited Aug 6 at 6:29


Ry-♦
161k34325345
161k34325345
asked Aug 6 at 6:23
japseow
536
536
1
use thedis
module to get the opcode for the operation and then you can dig throughceval.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
add a comment |Â
1
use thedis
module to get the opcode for the operation and then you can dig throughceval.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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Aug 6 at 6:39
user2357112
137k11141212
137k11141212
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
use the
dis
module to get the opcode for the operation and then you can dig throughceval.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