这个例子我是从博客上拿来的,但是我看不懂。
>>> class Udict:
def __init__(self, dict=None):
self.data = {}
if dict is not None: self.update(dict)
def update(self, dict=None):
self.data.update(dict)
print(self.data)
>>> d = {1:'one', 2:'two'}
>>> g = Udict(d)
{1: 'two', 2: 'two'}
>>> g.data
{1: 'two', 2: 'two'}
>>> c.Udict()
{}
>>> c.update({3: 'three'})
{3: 'three'}
>>>
我希望对 obj.update() 函数进行递归调用,直到'RuntimeError: maximum recursion depth exceeded'
但是不,update() 函数到达 print(self.data)
self.data.update(dict) 正在调用自身方法 update(),那么为什么它不像我预期的那样工作?
谁能给我解释一下为什么调用函数本身不会进入死循环?带有示例的博客没有解释它。
请您参考如下方法:
它根本没有递归地调用自己。
它不会在更新中调用 self.update:它会调用数据字典的更新方法,这完全是另一回事。
