str不可调用或int不可调用

时间:2016-06-20 10:49:07

标签: python

[已解决]我已经使用类在Python中创建了一个程序,但它不断地说'str' object is not callable,如果我尝试评论第7,8和16行,那么它说 'int' object is not callable

class Lambo:
    def __init__(self, color, model_number, average, milage):
        self.color = color
        self.model_number = model_number
        self.average = average
        self.milage = milage
    def color(self):
        print("Lamborghini color is " + self.color)
    def model_number(self):
        print("Lamborghini model number is " + str(self.model_number))
    def average(self):
        print("Lamborghini average is " + str(self.average))
    def milage(self):
        print("Lamborghini milage is " + str(self.milage))
lambor = Lambo("Red", 2016, 20, 8)
lambor.color()
lambor.model_number()
lambor.average()
lambor.milage()

预期产出:

Lamborghini color is Red
Lamborghini model_number is 2016
Lamborghini average is 20
Lamborghini milage is 8

我也在使用PyCharm社区版4.5.4

3 个答案:

答案 0 :(得分:3)

您正在使用color(等)作为变量名称(字符串)和方法名称。你不能在python中做到这一点。

错误str is not callable意味着您在字符串变量之后放置括号(因此尝试"调用"它就像一个函数)。无论何时看到这一点,编程逻辑都会出错。

答案 1 :(得分:3)

您的实例具有以下属性:

self.color = color
self.model_number = model_number
self.average = average
self.milage = milage

那些掩码你定义的方法;方法只是更多属性,但Python首先查看实例,然后才在类中查找属性:

>>> lambor = Lambo("Red", 2016, 20, 8)
>>> lambor.color
'Red'

您没有调用Lambo.color()方法,而是尝试调用字符串'Red'。重命名您在__init__中设置的属性,或重命名您的方法。

您可以为每个名称使用前导下划线来删除碰撞并记录该属性仅在您的类内部使用:

class Lambo:
    def __init__(self, color, model_number, average, milage):
        self._color = color
        self._model_number = model_number
        self._average = average
        self._milage = milage
    def color(self):
        print("Lamborghini color is " + self._color)
    def model_number(self):
        print("Lamborghini model number is " + str(self._model_number))
    def average(self):
        print("Lamborghini average is " + str(self._average))
    def milage(self):
        print("Lamborghini milage is " + str(self._milage))

或者,重命名方法以说明它正在做什么;用print_

作为前缀
class Lambo:
    def __init__(self, color, model_number, average, milage):
        self.color = color
        self.model_number = model_number
        self.average = average
        self.milage = milage
    def print_color(self):
        print("Lamborghini color is " + self.color)
    def print_model_number(self):
        print("Lamborghini model number is " + str(self.model_number))
    def print_average(self):
        print("Lamborghini average is " + str(self.average))
    def print_milage(self):
        print("Lamborghini milage is " + str(self.milage))

lambor = Lambo("Red", 2016, 20, 8)
lambor.print_color()
lambor.print_model_number()
lambor.print_average()
lambor.print_milage()

一般来说,你不应该让一个类负责这样的输出。您通常会为类提供一个__str__方法,该方法可以生成用户友好的字符串,然后在实例上打印调用str()或直接将其发送到print()函数:< / p>

class Lambo:
    def __init__(self, color, model_number, average, milage):
        self.color = color
        self.model_number = model_number
        self.average = average
        self.milage = milage
    def __str__(self):
        lines = [
            "Lamborghini color is " + self.color,
            "Lamborghini model number is " + str(self.model_number),
            "Lamborghini average is " + str(self.average),               
            "Lamborghini milage is " + str(self.milage)
        ]
        return '\n'.join(lines)

lambor = Lambo("Red", 2016, 20, 8)
print(lambor)

答案 2 :(得分:1)

您的方法和属性具有相同的名称。