學習筆記: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!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM