In ipdb, how to query a variable which as the same name as a command?
Asked Answered
L

2

5

I'm trying to debug a function quicksort(A, l, r) which has a local variable called l. However, in ipdb that also corresponds to a command to view the code around the current line. So I'm seeing something like this:

ipdb> dir()
['A', 'ipdb', 'l', 'r']
ipdb> A
[2, 4, 6, 1, 3, 5, 7, 8]
ipdb> l
     14         A[0], A[p] = A[p], A[0]
     15 
     16 def quicksort(A, l, r):
     17         # n = len(A)
     18         import ipdb; ipdb.set_trace()
---> 19         if len(A) == 1:
     20                 return
     21         else:
     22                 # choose_pivot(A)
     23                 q = partition(A, l, r)
     24                 quicksort(A, l, q-1)

What I actually want to do in this case is to see the value of l, however. Is there any way to 'escape' the default l command and see the value of the l variable?

Lieutenant answered 14/8, 2017 at 15:49 Comment(0)
L
3

I found that I can simply do p(l) to see the __repr__ representation (or print(l) to see the __str__ representation).

Lieutenant answered 14/8, 2017 at 15:53 Comment(0)
B
5

As suggested in this answer, you should prefix your statement with an exclamation mark !.

For instance:

ipdb> l                                                                                                                                                                                                                                                                   
    267 
    268         for i, l in enumerate(self.Q.net.layers):
    269 
    270             import ipdb; ipdb.set_trace()
    271 
--> 272             w, b = l.get_weights()
    273 
    274             res[f"W_{i}"] = wandb.Histogram(w.flatten(), num_bins=250)
    275             res[f"B_{i}"] = wandb.Histogram(b.flatten(), num_bins=250)
    276 
    277         wandb.log(res)

ipdb> !l                                                                                                                                                                                                                                                                  
<tensorflow.python.keras.layers.core.Dense object at 0x7fc28308e690>
ipdb>
Baronetcy answered 20/5, 2020 at 14:16 Comment(0)
L
3

I found that I can simply do p(l) to see the __repr__ representation (or print(l) to see the __str__ representation).

Lieutenant answered 14/8, 2017 at 15:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.