我有错误TypeError:'bool‘对象不可调用,当尝试在tinydb中使用函数search时
我的代码:
from tinydb import TinyDB, Query
db = TinyDB('db.json')
User = Query()
db.insert({'test': 'signs', 'age': 34})
res = db.search(User.test == 'signs')
print(res)`
发布于 2022-11-28 16:49:35
"test“似乎是查询对象的内建函数。将test更改为name或其他任何东西都会解决问题。
from tinydb import TinyDB, Query
db = TinyDB('db.json')
User = Query()
db.insert({'name': 'signs', 'age': 34})
res = db.search(User.name == "signs")
print(f"the search: {res}")
print(f"User.test: {User.test}")输出
the search: [{'name': 'signs', 'age': 34}]
User.test: <bound method Query.test of Query()>您还可以看到“test”作为Query对象的属性列出,在您创建的对象上运行dir()
>>> from tinydb import TinyDB, Query
>>> User = Query()
>>> dir(User)
['__and__', '__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__invert__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_generate_test', '_hash', '_path', '_test', 'all', 'any', 'exists', 'fragment', 'is_cacheable', 'map', 'matches', 'noop', 'one_of', 'search', 'test']https://stackoverflow.com/questions/74603001
复制相似问题