bhk...@gmail.com wrote: > I am trying to create 2D arrays without using advanced features like > numpy,
I'd say that using ctypes is a bit more "advanced" than *using* numpy because with ctypes it helps to know C. > for this I have created 2 separate modules arrays.py and > array2D.py. Here's the code for that: - You should absolutely go with numpy - If you insist on building your own use lists of lists as suggested - If you need something more space efficient than lists have a look at http://docs.python.org/2/library/array.html rather than ctypes. Now on to some debugging, without looking into the details. > import arrays > > class Array2D : > # Creates a 2-D array of size numRows x numCols. > def __init__( self, numRows, numCols ): > # Create a 1-D array to store an array reference for each row. > > self._theRows = arrays.Array( numRows ) Here you create an instance of your array of integers... > # Create the 1-D arrays for each row of the 2-D array. > print "Num of Cloumns is", numCols > > for i in range( numRows ) : > self._theRows[i] = arrays.Array( numCols ) ...and here you are stuffing arrays.Array instances into that array of integers. In nuce: >>> import ctypes >>> class C: pass # classic class ... >>> items = (ctypes.c_int * 3)() >>> items[0] = C() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: C instance has no attribute '__trunc__' ctypes unhelpfully tries to truncate the value to convert it into an integer. As an aside here's an example of a successful conversion: >>> class C2: ... def __trunc__(self): return 42 ... >>> items[0] = C2() >>> items[0] 42 There are basically two resolutions: - adjust the type of the outer array or - use a single 1D array internally and calculate the index as row * width + column (for example). -- http://mail.python.org/mailman/listinfo/python-list