ste*_*eve 1 python recursion function
我想问一下Python中的代码:
class UserDict:
def __init__(self, dict=None, **kwargs):
self.data = {}
if dict is not None:
self.update(dict)
if len(kwargs):
self.update(kwargs)
def clear(self): self.data.clear()
Run Code Online (Sandbox Code Playgroud)
这clear(self)是一个UserDict类的方法,并对类的data属性进行操作,对吧?这个功能不会data永远运作吗?因为它每次自称?
UserDict.clear()电话self.data.clear(). self.data是类型dict,不是UserDict,所以它调用不同的方法,而不是自己.如果UserDict.clear()调用self.clear()而不是,那将是无限递归self.data.clear().