PyList_SetItem与PyList_SETITEM [英] PyList_SetItem vs. PyList_SETITEM

查看:144
本文介绍了PyList_SetItem与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 ith 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 ith 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆