类方法 TypeError "Int object not callable"; [英] class method TypeError "Int object not callable"

查看:24
本文介绍了类方法 TypeError "Int object not callable";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TypeError: 'int' 对象不可调用

TypeError: 'int' object is not callable

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

推荐答案

如前所述,您在 __init__ 中为 increment_odometer 对象定义了一个名称,然后定义了一个具有相同名称的方法.只需删除 __init__

As mentioned you define a name for an increment_odometer object in the __init__ and later define a method with the same name. Just remove the self.increment_odometer = 0 in the __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()

这篇关于类方法 TypeError "Int object not callable";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆