[Numpy-discussion] performing operations in-place in numpy
Thu Jul 9 15:20:44 CDT 2009
On Thu, Jul 09, 2009 at 12:00:23PM -0500, Robert Kern wrote:
> On Thu, Jul 9, 2009 at 11:44, Fons Adriaensen<email@example.com> wrote:
> > There is a simple rule which says that if you use an object
> > pointer as a function argument you must INCREF it. This is
> > just the logical consequence of using refcounted objects.
> That's not true. There are many functions even in the standard Python
> C API that "borrow" a reference.
Indeed. Still, when calling a function Python will
INCREF the arguments. And that's only logical since
these arguments become local variables (i.e. a new
reference) inside the called function and that function
can do whatever it wants with them, including deleting
or re-assigning them. Creating a function argument is
not different from assigning to a new variable.
This is also the reason why sys.getrefcount() will return
a value that is one higher than the one in the caller's
*In principle* you need to do the same manually when
using any PyObject* as a function argument in C code.
Of course if you know that the called function will
do no harm, you can call it without an INCREF. In that
case you allow the function to use a borrowed reference.
If you don't know what the function is going to do, the
only safe way to call it is to INCREF the arguments.
Io lo dico sempre: l'Italia è troppo stretta e lunga.
More information about the NumPy-Discussion