【问题标题】:PyCharm and debugging private attributesPyCharm 和调试私有属性
【发布时间】:2017-12-24 01:31:06
【问题描述】:

我使用 PyCharm 社区版 3.4。

我已将self.__a 添加到手表。

这是我的例子:

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)

所以,我开始调试并在断点处停止。 self.__a 手表显示 {AttributeError}'Box' object has no attribute 'a'.

我按下 Alt + F8 并计算 self.__a = a。结果是None

然后我评估self.__a,结果是1

我的self.__a 手表仍然显示{AttributeError}'Box' object has no attribute 'a'。我删除它。然后我添加另一个手表self.__a。它显示1

你能解释一下这里发生了什么吗?

【问题讨论】:

    标签: python pycharm


    【解决方案1】:

    因为box没有__a

    它的名字被破坏了......

    另请参阅:Name_mangling#Python

    变成了

    Box._Box__a
    

    如果你把它改成观看

    self._Box__a 
    

    它会告诉你正确的值

    但它也应该显示在变量列表视图中(在调试运行面板的中心)

    【讨论】:

      猜你喜欢
      • 2012-02-10
      • 1970-01-01
      • 2011-02-13
      • 2010-09-29
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      • 1970-01-01
      相关资源
      最近更新 更多