Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43134
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' |
| Date | 2013-04-09 09:57 +0200 |
| Organization | None |
| References | <c1cc2d05-e718-4414-a7f3-c1e03621c4f5@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.326.1365494162.3114.python-list@python.org> (permalink) |
bhk755@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).
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-08 03:07 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' Dylan Evans <dylan@dje.me> - 2013-04-08 22:40 +1000
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-08 23:15 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-08 23:15 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-08 21:01 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-09 05:14 +0000
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-08 23:21 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' Peter Otten <__peter__@web.de> - 2013-04-09 09:57 +0200
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-09 01:50 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-09 01:50 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-09 01:48 -0700
csiph-web