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) 如果我有这个:
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
在C++中开发时,我使用这个非常有用的宏:
#define DD(a) std::cout << #a " = [ " << a << " ]" << std::endl;std::cout.flush();
Run Code Online (Sandbox Code Playgroud)
你能帮我在python中实现同样的想法吗?我不知道如何#a使用python函数实现...