Python 检查对象是 ListProxy 还是 DictProxy

标签 python multiprocessing typing

我正在 python 中使用多处理模块,并使用 mp 管理器提供的共享变量列表和字典。

import multiprocessing as mp    
a = mp.Manager()

b = a.list()
b
<ListProxy object, typeid 'list' at 0x14098207908>

c = a.dict()
c
<DictProxy object, typeid 'dict' at 0x140984579c8>

我想做:

if isinstance(b, ListProxy):
    do something
elif isinstance(c, DictProxy):
    do something else

但是这些似乎不是内置类型,我尝试根据基本列表和字典类型检查它们,但它们显然不起作用

如何在代码中测试这些类型?

最佳答案

试试这个。

import multiprocessing as mp

def judge(x):
    if isinstance(x, mp.managers.ListProxy):
        print('It is a ListProxy.')
    elif isinstance(x, mp.managers.DictProxy):
        print('It is a DictProxy.')
    else:
        pass
    pass

def main():
    a = mp.Manager()
    b = a.list()
    c = a.dict()
    judge(b)
    judge(c)
    pass

if __name__ == '__main__':
    main()
    pass

关于Python 检查对象是 ListProxy 还是 DictProxy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59713359/

相关文章:

python - Python 中 multiprocessing.manager 的替代方案

java - 链接方法时不能依赖目标类型

typing - 用于编程的两指非主行触摸打字是否可以接受?

python - 为什么 Python 不可变类型(如 int、str 或元组)需要使用 `__new__()` 而不仅仅是 `__init__()` ?

python - 使用 DataFrame 索引作为 x 轴刻度

python - 具有多处理卡住计算机的 Matplotlib

Python 输入类的联合及其实例

python - Pandas 图中单行的访问和更改特征

python - 如何统计某些条件下的某些数据

python - 如何使用 StdLib 和 Python 3 在一个范围内并行化迭代?