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

后端 未结 6 1215
没有蜡笔的小新
没有蜡笔的小新 2021-01-15 03:06

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

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


        
6条回答
  •  天命终不由人
    2021-01-15 04:03

    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
    

提交回复
热议问题