3

I currently have pdb imported in the calibrate function in a large python class called Machine. In another file, I create an instance of this class and run calibrate. I'm trying to access self.r_error which is a dictionary that points to a function, and it definitely exists and is normally recognized by the shell, but for some reason I can't iterate through it in a list:

(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

What's going on?


Here are some parts of the code that may or may not be relevant:

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()

And then in the shell, I ran python3 -i dsim_test.py. There weren't any errors, but self remains undefined precisely whenever it's in a list.

  • Could you post the ___init___ – Arghya Saha Aug 15 '18 at 14:47
  • I suspect that most of the `__init__` is irrelevant. What's most important is that it calls `self._init_error()`; everything else initializes values that aren't mentioned anywhere else in the featured code. – ArbitraryRenaissance Aug 15 '18 at 14:49
  • 1
    This is not related to `self`. It would not even work with `x = 1` followed by `[x for i in range(2)]`. See [List comprehension scope error from Python debugger](https://stackoverflow.com/questions/24149168/list-comprehension-scope-error-from-python-debugger) for answers. – zvone Aug 15 '18 at 14:51
  • Thank you! I think that solved my problem. – ArbitraryRenaissance Aug 15 '18 at 14:55

0 Answers0