加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – lambda函数中的未定义变量

发布时间:2020-12-20 11:08:04 所属栏目:Python 来源:网络整理
导读:我有代码: from functools import reducepublic_ids = [1,2,3,4,5]filepath = '/path/to/file/'rdd = sc.textFile( filepath)new_rdd = reduce( lambda a,b: a.filter( lambda x: b not in x ),public_ids,rdd) 此代码假定根据id列表过滤rdd中的行. rdd是使
我有代码:

from functools import reduce

public_ids = [1,2,3,4,5]
filepath = '/path/to/file/'

rdd = sc.textFile(
    filepath
)

new_rdd = reduce(
    lambda a,b: a.filter(
        lambda x: b not in x
    ),public_ids,rdd
)

此代码假定根据id列表过滤rdd中的行. rdd是使用spark context sc的textFile方法从位于filepath中的文件创建的.

此代码工作正常,但是pylint会引发错误:

E: Undefined variable ‘b’ (undefined-variable)

我相信我编码它的方式不是正确的方法.如何更改它,以便pylint不会再次引发错误?或者它只是一个结构,pylint无法正确识别?

解决方法

很可能这是pylint中的一个错误.

这是从2年前的similar bug report

foo = lambda x: lambda: x + 1 print(foo(1)())

correctly prints 2 when run,but pylint incorrectly reports

E: 1,24: Undefined variable 'x' (undefined-variable)

This is a regression from pylint 1.4.x.

这是一个recent issue在11/14/2018报道同一个问题

The issue has been reported at #760 and fixed by #2274. However,the
fix is merged only into pylint 2.x which supports only python >= 3.4
leaving us with the bug unresolved in the pylint 1.x series for python
2.

编辑

看起来你的误报可能与上面的问题略有不同,但是,我仍然认为这是一个错误.

我会尝试在他们的回购中创建一个问题,看看会发生什么(如果您决定这样做,请在评论中发布链接,以便我们可以关注它.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读