首页 > 解决方案 > 为什么在 pdb 中创建列表时无法访问 self?

问题描述

我目前已经pdbcalibrate一个名为Machine. 在另一个文件中,我创建了这个类的一个实例并运行calibrate. 我正在尝试访问self.r_error哪个是指向函数的字典,它肯定存在并且通常被 shell 识别,但由于某种原因,我无法在列表中遍历它:

(Pdb) self
<direct_simulation.Machine object at 0x033D7110>
(Pdb) self.r_error["xRx"](5)
1.9352496986596831e-10
(Pdb) y2 = [self.r_error["xRx"](i) for i in x]
*** NameError: name 'self' is not defined

这是怎么回事?


以下是一些可能相关或不相关的代码部分:

direct_simulation.py:

from collections import defaultdict
import numpy as np
class Machine(object):
    ...
    def _init_error(self):
        get5rands = lambda: [np.random.normal(0,1) for _ in range(5)]
        def rot_error_curve(a,b,c,d,e):
            def curve(x):
                ...
                (build a curve over the real numbers with a,b,c,d, and e)
                ...
            return curve
        self.r_error = defaultdict(lambda: rot_error_curve(*get5rands()),{})
    ...
    def calibrate(self):
        ...
        (things happen that create self.r_error["xRx"])
        ...
        import pdb;pdb.set_trace()

dsim_test.py:

import direct_simulation as ds
...
def do_cal():
    global mm
    mm = ds.Machine()
    mm.calibrate()
    ...
if __name__ == "__main__":
    do_cal()

然后在 shell 中,我跑了python3 -i dsim_test.py. 没有任何错误,但self只要它在列表中,它就保持未定义。

标签: pythonpdb

解决方案


推荐阅读