My question was not about printing a Dictionary.
What I want is, is there any way to print variable names along with values in Python?
Say I have the following C code snippet
#include <stdio.h>
typedef long long int ll;
#define getName(var) #var
#define debug(var) fprintf(stderr,"%s = %lld\n",getName(var),((ll)var));
int main()
{
int num = 5;
for(int iter = 0; iter<num; iter++)
debug(iter);
return 0;
}
There is nothing printed to stdout, but the following is printed to stderr.
iter = 0
iter = 1
iter = 2
iter = 3
iter = 4
In the same way, I would use stderr in python, but the syntax is little big.
from sys import stderr
for iter in range(5):
stderr.write("iter = "+str(iter)+'\n')
Even this code would write the same to stderr. But see the way I wrote it in C vs in python.
I need a simpler way to do this, if possible.
Please help @carre, @sebastian, @ssrivastava990
I am not sure if I understood your question correctly, but I think this is what you’re looking for.
from pdb import set_trace
And you call the set_trace() function anywhere in your code, with which you can check the values of any variable/expression upto that point of execution(usually done just above the line where you encounter an error)