python - 我正在学习 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'
有人可以向我解释吗?
解决方案
推荐阅读
- python - 使用 scipy.io.wavfile.write、librosa.output.write_wav 后得到扭曲的 WAV 文件
- php - Symfony 4:使用本地化日期
- vhdl - 如何在简单处理器中包含多路复用器?VHDL Altera Quartus II
- apache-kafka - 可从不同的应用程序 ID 访问的 QueryableStore
- uwp - 在本机 Windows 运行时组件/应用程序中解压缩 ZIP 存档
- javascript - 有没有办法让 JavaScript 中的对象调用自己的方法?
- python - 在 Python 中启用整数溢出
- javascript - 通过句柄获取脚本的 url(在 wordpress 中)
- node.js - 使用 Dynamoose 扫描所有记录非常慢
- c# - 如何计算 int32/64 的 int 和 float 乘积的限制?