Numpy's __array_interface__ not returning dict

boost, boost-python, c++, numpy, python

Solution

__array_interface__ should be a property (instance variable), not a method. So in the C++, or wherever the 'sprint.Matrix' object is defined, change it so that instead of having:

print X.__array_interface__
#<bound method Matrix.__array_interface__ of <sprint.Matrix object at 0x107c5c320>>

you have

print X.__array_interface__
#{'shape': (5, 5), 'data': (4416696960, True), 'typestr': '<f8'}

An alternative would be to define a custom wrapper class:

class SprintMatrixWrapper(object):
    def __init__(self, sprint_matrix):
        self.__array_interface__ = sprint_matrix.__array_interface__()

and then simply do:

numpy.array(SprintMatrixWrapper(X))

Problem

I am using an external program to compute a matrix that is written in C++ and is interfaced with python through `boost::python`. I would like to pass this C array to numpy, and according to the authors this ability is already implemented with numpy's `obj.__array_interface__`. If I call this in a python script and assign the C++ object to `X` I obtain the following: ``` print X #<sprint.Matrix object at 0x107c5c320> print X.__array_interface__ #<bound method Matrix.__array_interface__ of <sprint.Matrix object at 0x107c5c320>> print X.__array_interface__() #{'shape': (5, 5), 'data': (4416696960, True), 'typestr': '<f8'} print np.array(X) #Traceback (most recent call last): # File "<string>", line 96, in <module> #ValueError: Invalid __array_interface__ value, must be a dict ``` From my limited understanding I believe the problem is `X.__array_interface__` is not actually returning anything without the `()`. Is there a way to pass these arguments to `np.array` explicitly or a workaround for this issue. I am really quite new to mixing C++ and python, if this makes no sense or if I need to expound on any part let me know!

Original source