【Python】TypeError: ‘str‘ object is not callable(报错解决方案)

阅读 95

2022-01-25

项目场景

class Test():
    def __init__(self, color):
        self.color = color
        self.color()

    def color(self):
        print('color')


if __name__ == '__main__':
    Test('test')

问题描述

TypeError: 'str' object is not callable

原因分析

类属性与类方法重名,导致引用错误。

解决方案

避免类属性与类方法重名,修改为不同的值。

温馨提示

本文仅给出了引起 TypeError: 'str' object is not callable 报错的一种可能的情况(即类属性与类方法重名),其他原因也可能导致该错误。

引用参考

精彩评论(0)

0 0 举报