简体   繁体   English

为什么我得到错误'str' object is not callable

[英]Why do I get error 'str' object is not callable

class MyClassification:
    def __init__(self, lst2, search):
        self.lst2 = lst2
        self.search = search

    def search(self):
        if self.search in self.lst2:
            return True
        else:
            return False


var = MyClassification([1, 2, 3, 'Alex'], 'peter')
print(var.search())

I want to search if a name exists in a list or not but I get TypeError: 'str' object is not callable when I run print(var.search()) .我想搜索列表中是否存在名称,但我得到TypeError: 'str' object is not callable when I run print(var.search())

Please note that you used search as a parameter name and also as a method name, you should avoid naming both the same.请注意,您将搜索用作参数名称和方法名称,应避免将两者命名相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到'str' object is not callable? - Why do I get 'str' object is not callable? 为什么使用此代码会出现“TypeError: 'str' object is not callable”? - Why do I get "TypeError: 'str' object is not callable" with this code? 为什么在python(discord bot)中出现以下错误:TypeError:'str'对象不可调用 - Why do I get this following error in python(discord bot): TypeError: 'str' object is not callable 将变量追加到列表时,为什么会出现“'str'对象不可调用”的问题? - Why do I get “ 'str' object is not callable” when appending variables to a list? 为什么我会收到“浮动 object 不可调用”错误 - Why do I get 'Float object is not callable' error 为什么收到此错误“ TypeError:'int'对象不可调用”? - Why do i get this error “TypeError: 'int' object is not callable”? 我该如何解决这个“TypeError: ‘str’ object is not callable”错误? - How do I fix this "TypeError: 'str' object is not callable" error? 为什么我得到错误'str' object has no attribute 'head'? - Why do I get the error 'str' object has no attribute 'head'? 如何消除“'str'对象不可调用”? - How do I eliminate the “'str' object is not callable”? 为什么我会收到“模块”不可调用错误? - Why do I get the 'module' not callable error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM