0

Problem

In Julia, one could easily see the value of intermediate variable using @show marco for debugging purposes. For example

for i in 1:5
   @show i ^ 2
end

which will output

i ^ 2 = 1
i ^ 2 = 4
i ^ 2 = 9
i ^ 2 = 16
i ^ 2 = 25

However, in order to show intermediate value in Python, one have to write print("<variable> = " + ...), which is too much work for debugging a large project. I am wondering if there is any way that could enable Python to have similar functionality as in Julia.

I previously saw people use decorator to acquire the program runtime (see here), which is quite similar to Julia marco. But I do not know how to get it to work here to show intermediate variable.

Could someone help me, thank you in advance!

Mr.Robot
  • 349
  • 1
  • 16

2 Answers2

1

Why not just make a function (using python3):

def show(s):
    print(s + ' = ' + str(eval(s)))


for i in range(10):
    show(f"{i} ** 2")

This will output:

0 ** 2 = 0                                                                                                                                                     
1 ** 2 = 1                                                                                                                                                     
2 ** 2 = 4                                                                                                                                                     
3 ** 2 = 9                                                                                                                                                     
4 ** 2 = 16                                                                                                                                                    
5 ** 2 = 25                                                                                                                                                    
6 ** 2 = 36                                                                                                                                                    
7 ** 2 = 49                                                                                                                                                    
8 ** 2 = 64                                                                                                                                                    
9 ** 2 = 81

Still not as simple as julia but slightly easier to write than full on concatenation. Note that using eval() may be considered bad practice (see here and here).

As for using decorators, they can only be applied to functions or classes.

Perplexabot
  • 1,852
  • 3
  • 19
  • 22
0

you can use spyder, visual studio code or any IDE which supports debug mode.

Praveen
  • 346
  • 1
  • 6
  • 18