Python中文
首页
教程
问答
标签
搜索
登录
注册
Python类继承:递归E
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我试着把我的脑袋绕在课堂上,我看不出我在这个剧本里做错了什么。我已经构建了一些嵌套类。在</p> <pre><code>import random class Account(object): num_accounts = 0 def __init__(self, name, balance): self.name = name self.balance = balance Account.num_accounts += 1 def withdraw(self, amt): self.balance = self.balance - amt def inquiry(self): return self.balance class EvilAccount(Account): def __init__(self,name,balance,evilfactor): Account.__init__(self,name,balance) self.evilfactor = evilfactor def inquiry(self): if random.randint(0,4) == 1: return self.balance * self.evilfactor else: return self.balance class MoreEvilAccount(EvilAccount): def deposit(self,amount): self.withdraw(5.00) EvilAccount.deposit(self,amount) class WithdrawCharge(object): fee = 2.50 def withdraw_fee(self): self.withdraw(self.fee) class MostEvilAccount(EvilAccount, WithdrawCharge): def withdraw(self,amt): self.withdraw_fee() super(MostEvilAccount,self).withdraw(amt) </code></pre> <p>然后我用</p> ^{pr2}$ <p>一切都很好。但是当我试图调用继承的方法时:</p> <pre><code>d.withdraw(5.00) </code></pre> <p>我得到递归错误!在</p> <pre><code> File "StackO.py", line 37, in withdraw self.withdraw_fee() File "StackO.py", line 33, in withdraw_fee self.withdraw(self.fee) File "StackO.py", line 37, in withdraw self.withdraw_fee() RecursionError: maximum recursion depth exceeded </code></pre> <p>这是由davidm.Beazley撰写的Python基本参考,p121。在</p> <p>为什么我得到递归错误?在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>此错误用于防止堆栈溢出。有关如何工作的详细信息<a href="http://pythonforengineers.com/python-prevents-infinite-recursion-hence-stack-overflow/" rel="nofollow noreferrer">here</a></p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在python中从数据帧列中删除分类值?
3 回答
如何在python中从数据帧列表中删除引号
10 回答
如何在python中从数据帧创建列表
8 回答
如何在Python中从数据帧创建嵌套的JSON
2 回答
如何在Python中从数据帧显示wordcloud
3 回答
如何在Python中从数据帧的时间戳中删除字符
3 回答
如何在Python中从数据帧绘制简单绘图?
3 回答
如何在python中从数据帧行提取具有特定长度的范围?
1 回答
如何在python中从数据帧设置dict中的值
3 回答
如何在Python中从数据库中获得一个结果
3 回答
如何在python中从数据框中绘制分类条形图
7 回答
如何在Python中从数据框中选择特定细节?
4 回答
如何在python中从数据集中删除unicode
5 回答
如何在python中从数据集中删除某些数值?
3 回答
如何在python中从数据集中选择行
4 回答
如何在Python中从数组中删除元素
10 回答
如何在python中从数组中删除单个倒逗号?
1 回答
如何在python中从数组中删除对象?
2 回答
如何在python中从数组中删除引号
2 回答
如何在python中从数组中删除所有最小值
7 回答