我使用的是“学习python 3的图解指南”来学习python。第21章是关于类的。在本章中,它显然错误地使用了“自我”吗?我尝试编写自己的代码作为示例,但是它没有用,所以我输入了示例代码,令人惊讶的是,它也没有用。
class CorrectChair:
'''blah'''
max_occupants = 4
def __init__(self, id):
self.id = id
self.count = 0
def load(self, number):
new_val = self.check(self.count + number)
self.count = new_val
def unload(self, number):
new_val - self._check(self.count - number)
self.count = new_val
def _check(self, number):
if number < 0 or number > self.max_occupants:
raise ValueError('Invalid count:{}'.format(number))
return number
它错误地变成:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
CorrectChair.load(1)
TypeError: load() missing 1 required positional argument:
'number'
它似乎无法识别自我论点。如何解决此问题?谷歌搜索并没有帮助,我看到的每个示例都使它看起来应该可行。
它应该在self.count上加上(number),而不是忽略它的自我引用,并要求第二个参数。
绝地无双
慕田峪7331174
不负相思意
相关分类