python - 类方法 TypeError "Int object not callable"
问题描述
TypeError:“int”对象不可调用
class Car():
def __init__(self, make, model, year):
"""initialize attribuites to describe a car"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
self.increment_odometer = 0
def update_odometer(self, mileage):
"""
Set the odometer reading to the given value.
Reject the change if it attempts to roll the odometer back.
"""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You cannot roll back the odometer.")
def read_odometer(self):
"""print at statement which shows the car's miles"""
print("This car has " + str(self.odometer_reading) + " " + "miles on it.")
def get_descriptive_name(self):
"""Return a neatly formatted descriptive name."""
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
def increment_odometer(self, miles):
"""Add the given amount to the odometer reading."""
self.odometer_reading += miles
my_old_car = Car ('subaru', 'outback', 2013)
print (my_old_car.get_descriptive_name())
my_old_car.update_odometer(23500)
my_old_car.read_odometer()
my_old_car.increment_odometer(100)
my_old_car.read_odometer()
输出:
2013 Subaru Outback
This car has 23500 miles on it.
Traceback (most recent call last):
File "cars.py", line 42, in <module>
my_old_car.increment_odometer(100)
TypeError: 'int' object is not callable
解决方案
如前所述,您在中定义了一个 increment_odometer 对象的名称,__init__
然后定义了一个具有相同名称的方法。self.increment_odometer = 0
只需删除__init__
class Car():
def __init__(self, make, model, year):
"""initialize attribuites to describe a car"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
# self.increment_odometer = 0 ----> remove this line
def update_odometer(self, mileage):
"""
Set the odometer reading to the given value.
Reject the change if it attempts to roll the odometer back.
"""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You cannot roll back the odometer.")
def read_odometer(self):
"""print at statement which shows the car's miles"""
print("This car has " + str(self.odometer_reading) + " " + "miles on it.")
def get_descriptive_name(self):
"""Return a neatly formatted descriptive name."""
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
def increment_odometer(self, miles):
"""Add the given amount to the odometer reading."""
self.odometer_reading += miles
my_old_car = Car ('subaru', 'outback', 2013)
print (my_old_car.get_descriptive_name())
my_old_car.update_odometer(23500)
my_old_car.read_odometer()
my_old_car.increment_odometer(100)
my_old_car.read_odometer()
推荐阅读
- javascript - 无法弄清楚我做错了什么。未处理的拒绝(TypeError):无法读取未定义的属性“检查”
- node.js - 使用 AWS Lambda 函数模拟延迟
- flutter - Dio(用于颤振)-如何将数组 int 发送到 queryParameters?
- html - 角度背景换色器未知问题
- python - 从PDF文件中提取稀疏图?
- javascript - 如何将特定数组添加到数据库中的现有文档?
- javascript - Express-Postgres 无法应用数组过滤器:错误:运算符不存在:整数 [] && 文本 []
- python - AttributeError:“unicode”对象没有属性“strftime”
- javascript - 传递给网络工作者时,文件实例上的自定义属性未克隆
- python - tensorflow 2.3下的tensorflow-hub导入错误