This functionality requires iterating over the collection which is a destructive operation in some of them. This functionality would have been useful, however implementing the type checker within Python that would deal with complex types, nested type checking, type variables, string forward references, and so on is out of scope for this PEP.
from typing import List
import json
def extended_type(obj, buffer_list: List = None):
if buffer_list is None:
buffer_list = []
buffer_list.append(f"<class '{type(obj).__name__}'>")
if hasattr(obj, '__iter__') and not isinstance(obj, str):
sub_buf = []
for item in (obj.values() if isinstance(obj, dict) else obj):
extended_type(item, sub_buf)
if sub_buf:
buffer_list.append(sub_buf)
return buffer_list
否。类型提示和
typing
模块和PEP 585为此提供了一种表示法(即Python 3.9之前的list[tuple]
或List[tuple]
),但这些仅意味着由外部类型检查器(如MyPy)进行检查;没有在运行时检查它的功能PEP 585on使iInstance(obj,list[str])执行运行时类型检查:
容器项的类型对于容器本身的类型是不透明的。但是,您可以为实现
__iter__
的东西制作自己的:@Aplet123激励着我
试验
输出
相关问题 更多 >
编程相关推荐