相关疑难解决方法(0)


Python:打印变量的名称和值?

在调试时,我们经常会看到如下的print语句:

print x        # easy to type, but no context
print 'x=',x   # more context, harder to type
12
x= 12
Run Code Online (Sandbox Code Playgroud)

如何编写一个函数来获取变量或变量的名称并打印其名称和值?我只对调试输出感兴趣,这不会被合并到生产代码中.

debugPrint(x)    #  or
debugPrint('x')
x=12
Run Code Online (Sandbox Code Playgroud)

python debugging

36
推荐指数
5
解决办法
3万
查看次数

Python - 我可以访问给我打电话的对象吗?

如果我有这个:

class A:
    def callFunction(self, obj):
        obj.otherFunction()

class B:
    def callFunction(self, obj):
        obj.otherFunction()

class C:
    def otherFunction(self):
        # here I wan't to have acces to the instance of A or B who call me.

...

# in main or other object (not matter where)
a = A()
b = B()
c = C()
a.callFunction(c) # How 'c' know that is called by an instance of A...
b.callFunction(c) # ... or B
Run Code Online (Sandbox Code Playgroud)

尽管存在设计或其他问题,但这只是一个探究性思维的问题.

注意:必须在不更改 签名的情况下完成此操作otherFunction

python oop function-calls

3
推荐指数
1
解决办法
838
查看次数

你能把这个调试宏从C++翻译成python吗?

在C++中开发时,我使用这个非常有用的宏:

#define DD(a) std::cout << #a " = [ " << a << " ]" << std::endl;std::cout.flush();
Run Code Online (Sandbox Code Playgroud)

你能帮我在python中实现同样的想法吗?我不知道如何#a使用python函数实现...

c++ python debugging macros logging

2
推荐指数
2
解决办法
615
查看次数