Python 函数内的_repr_uu导致无限递归
我正在学习Python,并尝试编写以下代码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
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
=>AttributeDisplay.\uuuu repr\uuuu
=>AttributeDisplay.gatherAttributes.\uuuu repr\uuuu
(类的repr作为绑定方法repr的一部分)=>AttributeDisplay.\uuuu repr\uuuu
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
=>AttributeDisplay.\uuuu repr\uuuu
=>AttributeDisplay.gatherAttributes.\uuuu repr\uuuu
(类的repr作为绑定方法repr的一部分)=>AttributeDisplay.\uuuu repr\uuuu