Is one correct in stating the following:
If a Python object is created in a C function, but the function doesn\'t return it, no INCREF
is neede
First, read this more carefully, specifically the last paragraph, http://docs.python.org/extending/extending.html#ownership-rules
Easy way to think about it is thinking about the reference counts.
Your first statement is correct. If you create a new Python object (say PyLong
) then it already has a reference count of 1. This is fine if you're going to return it but if you're not going to return it, it needs to be garbage collected by Python and it is only marked for GC with refcount=0, thus you need to DECREF if you're not going to return it.
The second statement is false. If you need to return it and you created it, just return it. Returning transfers ownership. If you were to INCREF before returning, then you're telling Python that you also are retaining a copy. So again, if you create it, refcount=1. If you then do INCREF then refcount=2. But this is not what you want, you want to return with refcount=1.
I'm not quite sure I get this but this is more of a C related question. How are you adding an int
or double
to a Python object?
Can you give an example where that method won't work?
Again, I'm not sure when a C type is an attribute of a Python object. Every int
, double
, long
, etc. is wrapped by a Python object in some way or another.
The caveats to these answers are outlined in the link above. You really shouldn't even need my poor explanation after reading that. I hope I clarified and didn't confuse more.