python - 为什么在 pdb 中创建列表时无法访问 self?
问题描述
我目前已经pdb
在calibrate
一个名为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
只要它在列表中,它就保持未定义。
解决方案
推荐阅读
- android - 如何使用动画底部导航栏颤动进行导航
- raspberry-pi - Raspberry Pi Pico - 如何使用 rshell 和命令行安装库
- python - Coinbase API 标准 python 示例返回“无效签名”
- ios - 从组合中的不同发布者派生发布者
- ruby-on-rails - 在 Rails 中将字符串转换为 ActionDispatch::Http::UploadedFile 对象
- laravel - 使用 Laravel Sai 找不到测试数据库
- javascript - 深度克隆时,这种递归如何工作?
- java - 递归删除数组中的重复元素
- typescript - 与手动检查相比,什么最好留给类型来处理?
- nginx - nginx 服务器和 ssh 停止响应