不同之处在于元素的存储方式。
<U12 将它们平铺存储,将每个条目补零至长度为 12。要看到这一点,我们可以使用 tobytes 直接访问数据缓冲区:
>>> au
array(['french', 'mexican', 'cajun_creole', 'Ellipsis', 'southern_us',
'italian', 'thai'], dtype='<U12')
>>>
>>> sz = au.dtype.itemsize
>>> [au.tobytes()[i:i+sz].decode('utf32') for i in range(0, au.size * sz, sz)]
['french\x00\x00\x00\x00\x00\x00', 'mexican\x00\x00\x00\x00\x00', 'cajun_creole', 'Ellipsis\x00\x00\x00\x00', 'southern_us\x00', 'italian\x00\x00\x00\x00\x00', 'thai\x00\x00\x00\x00\x00\x00\x00\x00']
object 只存储对象引用,即指向str 对象的指针。我们可以使用以下事实来验证这一点——在当前的 CPython 实现中——id 返回一个 Python 对象的内存地址:
>>> ao
array(['french', 'mexican', 'cajun_creole', Ellipsis, 'southern_us',
'italian', 'thai'], dtype=object)
>>>
>>> sz = ao.dtype.itemsize
>>> [int.from_bytes(ao.tobytes()[i:i+sz], 'little') for i in range(0, ao.size * sz, sz)]
[140626141129896, 140625895652128, 140625895628080, 8856512, 140625895627504, 140626141132200, 140626343518024]
>>> [id(it) for it in ao]
[140626141129896, 140625895652128, 140625895628080, 8856512, 140625895627504, 140626141132200, 140626343518024]