1

I was recently debugging one of my large modules which has several internal (private) methods when I noticed in vscode debugger that the addresses of all internal methods was changing on every step through any code, even unrelated parts.

After worrying for a while that I had a serious bug and after a lot of head scratching and I managed to recreate the issue outside of vscode and without using a debugger with this:

class MyClass():

    def __init__(self):
        self._internal()

    def _internal(self):
        for i in range(10):
            print(i, f'id={id(self._internal)}')

x = MyClass()

If I keep running this sometimes the id of the _internal method is static but other-times I get output showing the _internal method changes address during the for loop:

> "\Program Files\Python310"\python internal_method.py
0 id=2298435929536
1 id=2298435929728
2 id=2298435929536
3 id=2298435929728
4 id=2298435929536
5 id=2298435929728
6 id=2298435929536
7 id=2298435929728
8 id=2298435929536
9 id=2298435929728

Note that in vscode debugger when stepping through the loop the addresses can change wildly:

>  c: && cd c:\Code\#Scrap && cmd /C ""C:\Program Files\Python310\python.exe" c:\Users\blah\.vscode\extensions\ms-python.python-2022.8.0\pythonFiles\lib\python\debugpy\launcher 50429 -- c:\Code\#Scrap\internal_method.py "
0 id=2341889160512
1 id=2341898208576
2 id=2341886959552
3 id=2341898111680
4 id=2341898210112
5 id=2341850994176
6 id=2341898298048
7 id=2341889399040
8 id=2341850998208
9 id=2341898172800

I thought that function addresses would remain static but perhaps I stumbled across a cpython language processing feature or optimisation, can anyone enlighten me?

mrkbutty
  • 489
  • 1
  • 5
  • 13

0 Answers0