我有这个在类中设置A或B的python代码。我想打印该类收到的内容:

if options.A:
   print "setting A"
   class TmpClass(A): pass
else:
   print "nothing set in the command line => setting to B"
   class TmpClass(B): pass

print "selected=",TmpClass


我想在输出中看到A或B,但看到:

selected= TmpClass

最佳答案

您的代码在做什么,用英语翻译是:

if option.A has a value that evaluates to True:
    define an empty class called "TmpClass" that inherits from the object called "A"
otherwise:
    define an empty class called "TmpClass" that inherits from the object called "B"


现在,如果代码的实际作用确实是您想要的,我想您想要的可能是知道您的类是基于A还是基于B的。最后是:

print('TmpClass inherits from : %s' % TmpClass.__bases__)


HTH!

关于python - 在python中打印变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8224341/

10-12 21:35