错误: TypeError: 'str' object is not callable,TypeError: 'int' object is not callable等

先说上面出现的原因:

TypeError: 'str' object is not callable:就是str不可以被系统调用,

TypeError: 'int' object is not callable:就是int不可以被系统调用

原因就是:你正在调用一个不能被调用的变量或对象,就是你调用函数、属性变量的方式错误。你可能即定义了一个名为dance(这个名称是随便取的,我看到网上大多都是用str这个作为列子和str()函数重名,没怎么说清楚,特别说明一下)的属性变量,还定义了一个dance函数,当调用dance函数的时候,就会出现上述的错误情况。
例子:

# 错误代码
class life(object):
    
    # 有dance属性变量
    def __init__(self, dance, eat):
   
        self.dance = dance
        self.eat = eat
    
    # 又有dance()函数
    def dance(self):
        return '跳舞{}次!'.format(self.dance)


    def eat(self):
        return '吃饭{}次!'.format(self.eat)


if __name__ == '__main__':

    someone = life(1, 3)
    # 调用dance()函数
    a = someone.dance()
    print(a)

正确代码:只用函数名和属性变量名不一样就好了

class life(object):

    def __init__(self, dance, eat):

        self.dance = dance
        self.eat = eat

    def dance_count(self):
        return '跳舞{}次!'.format(self.dance)


    def eat_count(self):
        return '吃饭{}次!'.format(self.eat)


if __name__ == '__main__':
    
    someone = life(1, 3)
    a = someone.dance_count()
    print(a)



# 或者是

class life(object):

    def __init__(self, dance, eat):

        self.dance1 = dance
        self.eat1 = eat

    def dance(self):
        return '跳舞{}次!'.format(self.dance1)


    def eat(self):
        return '吃饭{}次!'.format(self.eat1)


if __name__ == '__main__':

    someone = life(1, 3)
    a = someone.dance()
    print(a)

 

你可能感兴趣的:(python)