问题
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