学习笔记:RecursionError--Python递归深度报告

廖雪峰老师博客https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143186781871161bc8d6497004764b398401a401d4cce000

自己在编译器上写的时候没注意,self.score返回了函数而不是属性,于是出现recursionerror

class Student():

  @property
  def score(self):
    return self.score

  @score.setter
  def score(self,value):
    if not isinstance(value,int):
      raise ValueError('Bad Value')
    if value < 0 or value > 100:
      raise ValueError('Bad Value')
    self.score = value

if __name__ == '__main__':
  s = Student()
  s.score = 88
  print(s.score)

RecursionError: maximum recursion depth exceed

搜到一个处理方法:http://blog.csdn.net/gatieme/article/details/50443905

测试代码:

import sys

def func(depth):
  depth += 1
  print('Now the depth is %s' % depth)
  func(depth)                                             #  func递归的调用自己,永不退出,死循环

if __name__ == '__main__':
  func(0)

运行结果:

Now the depth is 997     # 好吧比代码作者的少2次

Traceback (most recent call last):

............................

修改Recursion Depth:

import sys

sys.setrecursionlimit(10**5)

再次运行studentproperty.py, RecursionError,  Again!

只好检查代码,发现错误,bad brain >*<

self.score ———> self._score

Done!

原文地址:https://www.cnblogs.com/lyu454978790/p/8622402.html