Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm and debugging private attributes

Tags:

python

pycharm

I use PyCharm Community Edition 3.4.

I have added self.__a to Watches.

This is my example:

class Box:
    def __init__(self, a, b, c):
        self.__a = a
        self._b  = b
        self.c   = c
        d = 0 #Breakpoint.


a = Box(1, 2, 3)

So, I start debugging and stop at the breakpoint. The self.__a watch shows {AttributeError}'Box' object has no attribute 'a'.

I press Alt + F8 and evaluate self.__a = a. The result is None.

Then I evaluate self.__a and the result is 1.

My watch for self.__a still shows {AttributeError}'Box' object has no attribute 'a'. I delete it. Then I add another watch self.__a. It shows 1.

Could you clarify what is going on here?

like image 397
Michael Avatar asked Dec 17 '14 23:12

Michael


1 Answers

it is because box has no __a

it gets name mangled ...

see also: Name_mangling#Python

it becomes

Box._Box__a

and if you change it to watch

self._Box__a 

it will show you the proper value

but it should also show up in the variables list view (in the center of the debug run panel)

like image 53
Joran Beasley Avatar answered Sep 26 '22 03:09

Joran Beasley