首页 > 解决方案 > 我正在学习 Python 中的多处理,我遇到了一个变量,它是“列表”和“对象”,这怎么可能?

问题描述

我正在测试的代码:

import time
import multiprocessing as mp
import funcoes

def f(n, a):
    n.value = 3.1415927
    for i in range(len(a)):
        a[i] = -a[i]

if __name__ == '__main__':
    num = mp.Value('d', 0.0)
    arr = mp.Array('i', range(10))

    p = mp.Process(target=f, args=[num, arr])
    p.start()

    print(arr[:])
    print(num.value)

    time.sleep(1)

    print(num.value)
    print(arr[:])

    print(type(arr[:]))

    print(arr)
    print(type(arr))

输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0.0

3.1415927
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

<class 'list'>

<SynchronizedArray wrapper for <multiprocessing.sharedctypes.c_long_Array_10 object at 0x000001A0857DD040>>
<class 'multiprocessing.sharedctypes.SynchronizedArray'>

怎么object和 同名list

只需[:]放在arr.


所以我做了一些测试:

arr = [1,2,3,4,5,6,7,8]

class arr:
    def test():
        print('anything')

print(type(arr.test))
print(type(arr))

print(type(arr()))
print(type(arr[:]))

输出:

<class 'function'>

<class 'type'>

<class '__main__.arr'>

#Error: 'type' object is not subscriptable


和另一个测试:

class arr:
    def test():
        print('anything')

arr = [1,2,3,4,5,6,7,8]

print(type(arr))
print(type(arr[:]))

print(type(arr()))
print(type(arr.test))

输出:

<class 'list'>

<class 'list'>

#Error: 'list' object is not callable

#Error: 'list' object has no attribute 'test'

有人可以向我解释吗?

标签: pythonmultiprocessing

解决方案


推荐阅读