Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数内的_repr_uu导致无限递归_Python_Recursion_Repr - Fatal编程技术网

Python 函数内的_repr_uu导致无限递归

Python 函数内的_repr_uu导致无限递归,python,recursion,repr,Python,Recursion,Repr,我正在学习Python,并尝试编写以下代码 class AttributeDisplay: '''Display all attributes of a class in __repr__. It can be inherited.''' def gatherAttributes(self): '''Gather all attributes and concatenate them as a string''' def g

我正在学习Python,并尝试编写以下代码

class AttributeDisplay:
    '''Display all attributes of a class in __repr__.
    It can be inherited.'''
    def gatherAttributes(self):
        '''Gather all attributes and concatenate them
        as a string'''
        def getAttributeNameAndValue(key):
            return '[%s] - [%s]' % (key, getattr(self, key))
        return '\n'.join((map(getAttributeNameAndValue, sorted(self.__dict__))))

    def __repr__(self):
        '''Print all attributes'''
        attr = self.gatherAttributes() # omitting () results in infinite recursion
        return '[Class: %s]\n%s' % (self.__class__.__name__, attr)
我不小心省略了括号,attr变成了一个函数而不是字符串。然而,当我调用
print(X)
时,它进入了无限递归

错误代码如下所示

def __repr__(self):
    '''Print all attributes'''
    attr = self.gatherAttributes # omitting () results in infinite recursion
    return '[Class: %s]\n%s' % (self.__class__.__name__, attr)
if __name__ == '__main__':
    class TopTest(AttributeDisplay):
        count = 0
        def __init__(self):
            self.attr1 = TopTest.count
            self.attr2 = TopTest.count+1
            TopTest.count += 2

    class SubTest(TopTest):
        pass

    def test():
        t1, t2 = TopTest(), SubTest()
        print(t1)
        print(t2)

    test()
文件“Some Folder/classtools.py”,第18行,在__

return '[Class: %s]\n%s' % (self.__class__.__name__, attr)
[上一行重复244次]

RecursionError:调用Python对象时超出了最大递归深度

我试图调试,但找不到这种行为的确切原因。即使我不小心离开了括号,它也应该打印
对吗

在这种情况下,为什么它在
\uuu repr\uu
中调用自己

提前谢谢

编辑:测试代码如下所示

def __repr__(self):
    '''Print all attributes'''
    attr = self.gatherAttributes # omitting () results in infinite recursion
    return '[Class: %s]\n%s' % (self.__class__.__name__, attr)
if __name__ == '__main__':
    class TopTest(AttributeDisplay):
        count = 0
        def __init__(self):
            self.attr1 = TopTest.count
            self.attr2 = TopTest.count+1
            TopTest.count += 2

    class SubTest(TopTest):
        pass

    def test():
        t1, t2 = TopTest(), SubTest()
        print(t1)
        print(t2)

    test()

这是因为绑定方法的
repr
包含它绑定到的对象:

>>> class C:
...     def __repr__(self):
...         return '<repr here!>'
...     def x(self): pass
... 
>>> C().x
<bound method C.x of <repr here!>>
>>> str(C().x)
'<bound method C.x of <repr here!>>'
>>C类:
...     定义报告(自我):
...         返回“”
...     def x(自身):通过
... 
>>>C.x
>>>str(C().x)
''
请注意,我在这里做了一些飞跃——它们是:

  • '%s'%x
    大致相当于
    str(x)
  • 当某些内容没有定义
    \uuuu str\uuuu
    时,它会返回到
    \uuuu repr\uuuu
    (方法描述符就是这种情况)
在检索类的repr时,您将以以下循环结束:

  • AttributeDisplay.\uuuu repr\uuuu
    =>
  • AttributeDisplay.gatherAttributes.\uuuu repr\uuuu
    =>
  • AttributeDisplay.\uuuu repr\uuuu
    (类的repr作为绑定方法repr的一部分)=>

这是因为绑定方法的
repr
包括它绑定到的对象:

>>> class C:
...     def __repr__(self):
...         return '<repr here!>'
...     def x(self): pass
... 
>>> C().x
<bound method C.x of <repr here!>>
>>> str(C().x)
'<bound method C.x of <repr here!>>'
>>C类:
...     定义报告(自我):
...         返回“”
...     def x(自身):通过
... 
>>>C.x
>>>str(C().x)
''
请注意,我在这里做了一些飞跃——它们是:

  • '%s'%x
    大致相当于
    str(x)
  • 当某些内容没有定义
    \uuuu str\uuuu
    时,它会返回到
    \uuuu repr\uuuu
    (方法描述符就是这种情况)
在检索类的repr时,您将以以下循环结束:

  • AttributeDisplay.\uuuu repr\uuuu
    =>
  • AttributeDisplay.gatherAttributes.\uuuu repr\uuuu
    =>
  • AttributeDisplay.\uuuu repr\uuuu
    (类的repr作为绑定方法repr的一部分)=>

这个问题不清楚。你是如何得到递归的?这个问题还不清楚。你是如何得到递归的?