[C++-sig] create boost array from a Numpy PyArray without copying data?

AFoglia at princeton.com AFoglia at princeton.com
Fri Jan 23 18:03:38 CET 2009


Sebastian Walter wrote:

>>> And is there a way to get the refcount of an object within Python? I'd
>>> like to test if the refcount is correct.
>> sys.getrefcount(obj)
> 
> Good to know. When I run
> - ------------------- test.py ----------------
> from _test import *
> import sys
> 
> def test_my_array():
> 	a = A()
> 	print 'a.my_array=',a.my_array
> 	print 'sys.getrefcount(a)=',sys.getrefcount(a)
> 	print 'sys.getrefcount(a.my_array)=',sys.getrefcount(a.my_array)
> 
> 
> if __name__ == "__main__":
> 	test_my_array()
> 
> - ------------ end test.py -------------
> I get the output
> 
> - ----------- output -------------
> 
> walter at wronski$ python test.py
> a.my_array= [ 1.  2.  3.]
> sys.getrefcount(a)= 2
> sys.getrefcount(a.my_array)= 2
> 
> - ----------- end output --------
> 
> Ermm, is that good?
> I expected that the refcount would be 1 and not 2.

When you call getrefcount, the argument becomes another reference, so 
there are two references.

http://docs.python.org/library/sys.html#sys.getrefcount

sys.getrefcount(object)
Return the reference count of the object. The count returned is 
generally one higher than you might expect, because it includes the 
(temporary) reference as an argument to getrefcount().


-- 
Anthony Foglia
Princeton Consultants



More information about the Cplusplus-sig mailing list