0
点赞
收藏
分享

微信扫一扫

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

何晓杰Dev 2022-01-25 阅读 95

项目场景

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 条评论