PythonTypeError: ‘str‘ object is not callable(报错解决方案)

Posted Xavier Jiezou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PythonTypeError: ‘str‘ object is not callable(报错解决方案)相关的知识,希望对你有一定的参考价值。

项目场景

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 报错的一种可能的情况(即类属性与类方法重名),其他原因也可能导致该错误。

引用参考

http://www.jsphp.net/python/show-24-204-1.html

以上是关于PythonTypeError: ‘str‘ object is not callable(报错解决方案)的主要内容,如果未能解决你的问题,请参考以下文章