python recursion_学习笔记: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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值