0
파이썬을 배우려고하는데 마지막 문이 무한 재귀 호출을 초래하는 이유를 모르겠습니다. 당신이 self.children
을 할당하지 않기 때문에 누군가가파이썬에서 중첩 된 객체를 재귀 적으로 인쇄하는 방법은 무엇입니까?
class Container:
tag = 'container'
children = []
def add(self,child):
self.children.append(child)
def __str__(self):
result = '<'+self.tag+'>'
for child in self.children:
result += str(child)
result += '<'+self.tag+'/>'
return result
class SubContainer(Container):
tag = 'sub'
c = Container()
d = SubContainer()
c.add(d)
print(c)
Offcourse! 고마워. –
참고로 클래스 및 인스턴스 속성의 차이점에 대한 질문 링크는 다음과 같습니다. http://stackoverflow.com/questions/207000/python-difference-between-class-and-instance-attributes – jdi