Python nltk.tree.tree对象如何生成树的字符串表示形式?

Python nltk.tree.tree对象如何生成树的字符串表示形式?,python,nltk,Python,Nltk,我试图理解nltk.tree模块。我不明白为什么打印nltk.tree.tree对象时,它不会打印出地址。相反,它打印出树的字符串表示形式 我已经看过nltk.tree中的源代码,但我不知道代码的哪一部分将树格式化为字符串 import nltk from nltk.tree import Tree print(Tree(1, [2, Tree(3, [4]), 5])) print(type(Tree(1, [2, Tree(3, [4]), 5]))) 输出 (1 2 (3 4) 5) &

我试图理解nltk.tree模块。我不明白为什么打印nltk.tree.tree对象时,它不会打印出地址。相反,它打印出树的字符串表示形式

我已经看过nltk.tree中的源代码,但我不知道代码的哪一部分将树格式化为字符串

import nltk
from nltk.tree import Tree
print(Tree(1, [2, Tree(3, [4]), 5]))
print(type(Tree(1, [2, Tree(3, [4]), 5])))
输出

(1 2 (3 4) 5)
<class 'nltk.tree.Tree'>
输出:

<__main__.example object at 0x000001397F255668>
<class '__main__.example'>


我明白了。为什么会这样?

说得很清楚,我想问题是,为什么NLTK中
对象的输入是整数,但在打印时,表示会打印出字符串,而不会产生任何错误

让我们深入了解一下代码

以人类可读的括号解析格式打印
树的部分是位于

如果我们仔细观察,它会调用
pformat()
函数:

    def __str__(self):
        return self.pformat()
pformat()
函数位于:

我们看到它来自
nltk.compat
from

在Python3中,
nltk.compat.unicode_repr
只返回默认为unicode的
repr
,特别是
utf8
IIRC

但在Python 2中,它首先检查对象是否具有
unicode\u repr()
monkey patch函数

然后它从
six
库中检查它是
text\u type
的一种类型,如果是,它将打印出不带
u
前缀的输出,例如
u“…”

最后,它是Python2,对象没有
unicode\u repr()
,也不是
six.text类型,它只需打印
repr(obj)

回到问题,在对象是整数的情况下,
repr(int)
将转换为字符串

>>> type(1)
<class 'int'>
>>> repr(1)
'1'
>>> type(repr(1))
<class 'str'>
>类型(1)
>>>报告(1)
'1'
>>>类型(报告(1))

要清楚,我想问题是,为什么NLTK中
对象的输入是整数,但在打印时,表示将打印字符串,而不会产生任何错误

让我们深入了解一下代码

以人类可读的括号解析格式打印
树的部分是位于

如果我们仔细观察,它会调用
pformat()
函数:

    def __str__(self):
        return self.pformat()
pformat()
函数位于:

我们看到它来自
nltk.compat
from

在Python3中,
nltk.compat.unicode_repr
只返回默认为unicode的
repr
,特别是
utf8
IIRC

但在Python 2中,它首先检查对象是否具有
unicode\u repr()
monkey patch函数

然后它从
six
库中检查它是
text\u type
的一种类型,如果是,它将打印出不带
u
前缀的输出,例如
u“…”

最后,它是Python2,对象没有
unicode\u repr()
,也不是
six.text类型,它只需打印
repr(obj)

回到问题,在对象是整数的情况下,
repr(int)
将转换为字符串

>>> type(1)
<class 'int'>
>>> repr(1)
'1'
>>> type(repr(1))
<class 'str'>
>类型(1)
>>>报告(1)
'1'
>>>类型(报告(1))

你的问题是什么?好问题!=)你的问题是什么?好问题!=)
def unicode_repr(obj):
    """
    For classes that was fixed with @python_2_unicode_compatible
    ``unicode_repr`` returns ``obj.unicode_repr()``; for unicode strings
    the result is returned without "u" letter (to make output the
    same under Python 2.x and Python 3.x); for other variables
    it is the same as ``repr``.
    """
    if PY3:
        return repr(obj)

    # Python 2.x
    if hasattr(obj, 'unicode_repr'):
        return obj.unicode_repr()

    if isinstance(obj, text_type):
        return repr(obj)[1:]  # strip "u" letter from output

    return repr(obj)
>>> type(1)
<class 'int'>
>>> repr(1)
'1'
>>> type(repr(1))
<class 'str'>