当我尝试运行这段代码时:
from pprint import PrettyPrinter
class MyPrettyPrinter(PrettyPrinter):
def __init__(self, *args, **kwargs):
PrettyPrinter.__init__(self, *args, **kwargs)
def format(self, object, context, maxlevels, level):
(repr, readable, recursive) = PrettyPrinter.format(self, object, context, maxlevels, level)
return (type(repr)(object), readable, recursive) if isinstance(object, str) else (repr, readable, recursive)
print(MyPrettyPrinter().pformat(['x']))
我在Python 3中得到了不同的输出结果(['x']
),而在Python 2中得到了不同的输出结果([x]
)。
为什么会这样,如何才能获得与Python 2相同的行为?