RethinkDB:​​“TypeError:'Var' 对象不可调用";在过滤器中使用 lambda 函数时 [英] RethinkDB: "TypeError: 'Var' object is not callable" when using lambda function in filter

查看:50
本文介绍了RethinkDB:​​“TypeError:'Var' 对象不可调用";在过滤器中使用 lambda 函数时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 RethinkDB 的数据浏览器中,我使用 javascript 成功运行了此查询:

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.")})

但是当我像这样在 python 中相应地运行它时:

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."))

我收到以下错误:

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 = {}

为什么..?

推荐答案

在 Python 中,您应该使用方括号来访问字段.JavaScript 驱动程序使用括号是因为您不能在 JS 中重载方括号.

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."))

这篇关于RethinkDB:​​“TypeError:'Var' 对象不可调用";在过滤器中使用 lambda 函数时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆