Can you translate this debug macro from C ++ to python?

I use this very useful macro when developing in C ++:

#define DD(a) std::cout << #a " = [ " << a << " ]" << std::endl;std::cout.flush(); 

Could you help me implement the same idea in python? I don't know how #a can be implemented using python function ...

+2
source share
6 answers

You can check the stack trace and "parse" it. Since you know the name of your function (dd in this case), it becomes quite easy to find the call and extract the variable name.

  import inspect import re def dd(value): calling_frame_record = inspect.stack()[1] frame = inspect.getframeinfo(calling_frame_record[0]) m = re.search( "dd\((.+)\)", frame.code_context[0]) if m: print "{0} = {1}".format(m.group(1), value) def test(): a = 4 dd(a) test() 

Exit

 a = 4 
+6
source

Like @Andrea Spadaccini and @adirau , it is not possible to reliably map values โ€‹โ€‹back to Python variable names. You can wade through all namespaces that look for a variable name that refers to a given value, but it will be a struggle with the system and the ability to return the wrong variable name.

It is much easier to just pass the variable name:

 import inspect def pv(name): frame,filename,line_number,function_name,lines,index=inspect.getouterframes( inspect.currentframe())[1] # print(frame,filename,line_number,function_name,lines,index) val=eval(name,frame.f_globals,frame.f_locals) print('{0}: {1}'.format(name, val)) a=5 pv('a') 

gives:

 a: 5 
+7
source

I think this cannot be done.

The debug mask you posted works because it expands before compilation during preprocessing when you know the name of the variable. It is like you are writing all these cout yourself.

Python does not have a preprocessor (AFAIK), there are external tools that do the same thing (pyp and others), but you cannot define a macro with a standard language.

So you have to do your trick at runtime. Well, at run time, you donโ€™t know the "name" of the variable, because the variable is just a reference to the object when you call the method that you call it on the object, not the "variable". There can be many variables pointing to this object, how does the object know which variable was used to call the method?

+3
source

You cannot get a variable name (well, an object) in python. But you can pass the name of the object to get its value (something opposite to what you do with this macro)

 >>> a=4 >>> locals()['a'] 4 

EDIT: A detailed explanation can be found here.

+3
source
 import sys def DD(expr): frame = sys._getframe(1) print '%s = %s' % (expr, repr(eval(expr, frame.f_globals, frame.f_locals))) GLOBAL_VAR = 10 def test(): local_var = 20 DD('GLOBAL_VAR + local_var') >>> test() GLOBAL_VAR + local_var = 30 
+3
source

The Rod solution is great for use. It could even be expanded to handle many vars. But you can come close to this with much less magic:

 def dd(**kwargs): print ", ".join(str(k) + "=" + str(v) for k, v in kwargs.iteritems()) a = 1 dd(a=a,b=3) 

exit:

 a=1, b=3 
+2
source

All Articles