python - python3列表过滤器lambda获取TypeError:'wrapper_descriptor'对象不可下标
问题描述
以下代码在 python2 中有效,但在 python3 中无效。我需要一些帮助,谢谢。
class A:
a = (0, "a")
b = (1, "b")
choice = 1
hit = len(list(filter(lambda x: getattr(A, x) is not None and getattr(A, x)[0]==choice, dir(A))))>0
print(hit)
在蟒蛇 2
$ python2 --version
Python 2.7.16
$ python2 b.py
True
但是在python3
$ python3 --version
Python 3.9.0
$ python3 b.py
Traceback (most recent call last):
File "/private/tmp/a/b.py", line 7, in <module>
hit = len(list(filter(lambda x: getattr(A, x) is not None and getattr(A, x)[0]==choice, dir(A))))>0
File "/private/tmp/a/b.py", line 7, in <lambda>
hit = len(list(filter(lambda x: getattr(A, x) is not None and getattr(A, x)[0]==choice, dir(A))))>0
TypeError: 'wrapper_descriptor' object is not subscriptable
解决方案
您看到 Python 2 和 Python 3 之间存在差异的原因是,在 Python 3 中,类隐式继承了object
该类。在 Python 2 中,您有:
>>> dir(A)
['__doc__', '__module__', 'a', 'b']
>>>
在这种情况下,两者都__doc__
具有__module__
value None
,因此您的过滤器测试有效。
但在 Python 3 中,您也拥有object
该类的所有内容:
>>> dir(A)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b']
>>>
其中一些属性既不是None
也不是可下标的。例如,__class__
属性是一种类型,并且不可下标,这可能是您的错误的来源。
理想情况下,您将使用已知的属性列表,而不依赖于dir()
,但如果您想检查所有内容,您可以执行一些操作,例如忽略以 . 开头的属性__
。那可以代替is not None
测试。例如,以下代码适用于 Python 2 和 Python 3:
hit = len(list(filter(lambda x: not x.startswith("__") and getattr(A, x)[0]==choice, dir(A))))>0
推荐阅读
- javascript - 如何以多步骤形式将 URL 中的电子邮件添加到隐藏的电子邮件表单字段?
- python - 复制后奇怪的numpy数组赋值
- java - 删除数组中的字符串
- python - python:将每个元素转换为列表中的字符串,而不使用for循环
- python - 如何让不和谐 api 忽略不和谐用户?
- bitbucket - AWS Code PipeLine 未获取 BitBucket 帐户的所有存储库
- docker - 我的反应 docker 网页没有绑定到主机
- c# - 将学生的实例存储到数组中
- excel - Excel - 在表列中使用切片器和小计数字,同时忽略用另一列标识的重复值
- c++ - 我可以制作一个使用 epoll_wait 等待但在条件变量发生变化时立即返回的自定义服务员吗?