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.
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?
A list that calls a function every time it is mutated.
List object is the more general sequence provided by Python. Lists are ordered collections of arbitrarily typed objects. They have no fixed size. In other words, they can hold arbitrary objects and can expand dynamically as new items are added.
List Index. We can use the index operator [] to access an item in a list. In Python, indices start at 0. So, a list having 5 elements will have an index from 0 to 4.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With