小编Olg*_*lga的帖子

为什么有些Python字符串是用引号打印的,有些是用引号打印的?

我有字符串表示问题.我正在尝试打印我的对象,有时我会在输出中得到单引号.请帮助我理解它为什么会发生,如何打印出没有引号的对象.

这是我的代码:

class Tree:
    def __init__(self, value, *children):
        self.value = value
        self.children = list(children)
        self.marker = ""

    def __repr__(self):
        if len(self.children) == 0:
            return '%s' %self.value
        else:
            childrenStr = ' '.join(map(repr, self.children))
            return '(%s %s)' % (self.value, childrenStr)
Run Code Online (Sandbox Code Playgroud)

这是我做的:

from Tree import Tree
t = Tree('X', Tree('Y','y'), Tree('Z', 'z'))
print t
Run Code Online (Sandbox Code Playgroud)

这是我得到的:

(X (Y 'y') (Z 'z'))
Run Code Online (Sandbox Code Playgroud)

这是我想要的:

(X (Y y) (Z z))
Run Code Online (Sandbox Code Playgroud)

为什么引号出现在终端节点的值周围,而不是在非终端的值周围?

python string representation

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×1

representation ×1

string ×1