PyList_SetItem与PyList_SETITEM [英] PyList_SetItem vs. PyList_SETITEM
问题描述
据我所知,PyList_SetItem和PyList_SETITEM之间的区别在于PyList_SetItem会降低其覆盖的列表项的引用计数,而PyList_SETITEM不会.
From what I can tell, the difference between PyList_SetItem and PyList_SETITEM is that PyList_SetItem will lower the reference count of the list item it overwrites and PyList_SETITEM does not.
我有什么理由不总是一直使用PyList_SetItem吗?还是使用PyList_SetItem初始化列表中的索引位置会麻烦吗?
Is there any reason why I shouldn't just use PyList_SetItem all the time? Or would I get into trouble if I used PyList_SetItem to initialize an index position in a list?
推荐答案
PyList_SET_ITEM
是一个不安全的宏,它基本上将对象粘贴到列表的内部指针数组中,而没有任何绑定检查.如果在列表的第i
个位置中没有任何NULL
,将发生参考泄漏. PyList_SET_ITEM
窃取对您放入列表中的对象的引用. PyList_SetItem
也会窃取引用,但是会检查范围并拒绝对可能位于i
位置的任何内容进行引用.经验法则是使用PyList_SET_ITEM
初始化刚创建的列表,否则使用PyList_SetItem
初始化.在任何地方使用PyList_SetItem
也是完全安全的; PyList_SET_ITEM
本质上是一种速度突破.
PyList_SET_ITEM
is an unsafe macro that basically sticks an object into the list's internal pointer array without any bound checks. If anything non-NULL
is in the i
th position of the list, a reference leak will occur. PyList_SET_ITEM
steals the reference to the object you put in the list. PyList_SetItem
also steals the reference, but it checks bounds and decrefs anything which may be in the i
th position. The rule-of-thumb is use PyList_SET_ITEM
to initialize lists you've just created and PyList_SetItem
otherwise. It's also completely safe to use PyList_SetItem
everywhere; PyList_SET_ITEM
is basically a speed hack.
这篇关于PyList_SetItem与PyList_SETITEM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!