RethinkDB: “TypeError: 'Var' object is not callable” when using lambda function in filter

百般思念 提交于 2019-12-11 02:46:14

问题


In RethinkDB's data explorer, I'm running this query successfully using javascript:

r.db('my_db').table('my_table').filter(function(row){return row('some_key').match(".sometext.")})

But when I'm running it correspondingly in python like this:

r.db('my_db').table('my_table').filter(lambda row: row('some_key').match(".sometext."))

I'm getting the following error:

TypeError: 'Var' object is not callable

In [16]: rql = r.db('my_db').table('my_table').filter(lambda row: row('some_key').match(".sometext."))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-09fcb943624a> in <module>()
----> 1 rql = r.db('my_db').table('my_table').filter(lambda row: row('some_key').match(".sometext."))

/usr/lib64/python2.7/site-packages/rethinkdb/ast.pyc in filter(self, func, default)
    348 
    349     def filter(self, func, default=()):
--> 350         return Filter(self, func_wrap(func), default=default)
    351 
    352     def concat_map(self, func):

/usr/lib64/python2.7/site-packages/rethinkdb/ast.pyc in func_wrap(val)
   1161 # Called on arguments that should be functions
   1162 def func_wrap(val):
-> 1163     val = expr(val)
   1164 
   1165     # Scan for IMPLICIT_VAR or JS

/usr/lib64/python2.7/site-packages/rethinkdb/ast.pyc in expr(val, nesting_depth)
     43         return MakeObj(obj)
     44     elif isinstance(val, collections.Callable):
---> 45         return Func(val)
     46     else:
     47         return Datum(val)

/usr/lib64/python2.7/site-packages/rethinkdb/ast.pyc in __init__(self, lmbd)
   1198 
   1199         self.vrs = vrs
-> 1200         self.args = [MakeArray(*vrids), expr(lmbd(*vrs))]
   1201         self.optargs = {}

WHY..?


回答1:


With python you should use the square bracket to access a field. The JavaScript driver uses parentheses because you can't overload the square bracket in JS.

That's what your query should look like.

r.db('my_db').table('my_table').filter(lambda row: row['some_key'].match(".sometext."))


来源:https://stackoverflow.com/questions/21801054/rethinkdb-typeerror-var-object-is-not-callable-when-using-lambda-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!