5

In python pdb I set a breakpoint in code and run my file. It goes into debug mode as usual. I wanted to print the output of a map call. Hence I typed in the following in pdb:

(Pdb) p inp, pos
('abcde', [0, 1, 2])
(Pdb) map(lambda x: inp[x], pos)
*** NameError: global name 'inp' is not defined
(Pdb) 

I don't understand the NameError exception in here...what is the correct way to do a map call in pdb?

Update:

Here is an output which may help you reproduce the problem:

$ cat reproduce.py
from itertools import combinations

def comb(seq, r):
    n = len(seq)
    m = range(n)
    vectors = list(combinations(m, r))
    result = []
    for v in vectors:
        result.append(
            map(lambda x: seq[x], v)
            )
    return result

if __name__ == '__main__':

    r = comb('abcde', 3)
    import pprint as pp ; pp.pprint(r)
$ python -m pdb reproduce.py
> /home/deostroll/Public/code/py/reproduce.py(1)<module>()
-> from itertools import combinations
(Pdb) b 10
Breakpoint 1 at /home/deostroll/Public/code/py/reproduce.py:10
(Pdb) c
> /home/deostroll/Public/code/py/reproduce.py(10)comb()
-> map(lambda x: seq[x], v)
(Pdb) p v, seq
((0, 1, 2), 'abcde')
(Pdb) map(lambda z: seq[z], v)
*** NameError: global name 'seq' is not defined
(Pdb) 
deostroll
  • 11,661
  • 21
  • 90
  • 161
  • I ... don't understand it either :-). It feels like a bug to me. – mgilson Feb 23 '15 at 03:25
  • This seems like a very interesting problem, but I can't reproduce. Could you provide a [MCVE](http://stackoverflow.com/help/mcve)? – Kevin Feb 23 '15 at 15:41
  • @Kevin now you have it... – deostroll Feb 23 '15 at 17:59
  • The solution from the duplicate would be to run `eval("map(lambda z: seq[z], v)", vars())`or, if you want something prettier, [`!import code; code.interact(local=vars())`](http://stackoverflow.com/a/8387484/1763356). – Veedrac Feb 23 '15 at 20:03

0 Answers0