我尝试编写一个小类,并希望根据重量对项目进行排序。以下是提供的代码:
当我运行程序时,我得到了Python TypeError: sort() takes no positional arguments的错误堆栈。这是什么问题?
class Bird:
def __init__(self, weight):
# __weight for the private variable
self.__weight = weight
def weight(self):
return self.__weight
def __repr__(self):
return "Bird, weight = " + str(self.__weight)
if __name__ == '__main__':
# Create a list of Bird objects.
birds = []
birds.append(Bird(10))
birds.append(Bird(5))
birds.append(Bird(200))
# Sort the birds by their weights.
birds.sort(lambda b: b.weight())
# Display sorted birds.
for b in birds:
print(b)
当我运行程序时,我得到了Python TypeError: sort() takes no positional arguments的错误堆栈。这是什么问题?