[PYTHON MATRIX-SIG] bug: assignment from string

Jim Fulton, U.S. Geological Survey jfulton@usgs.gov
Tue, 19 Dec 1995 09:50:26 -0500


On Dec 18,  3:56pm, James Hugunin wrote:
> Subject: Re: [PYTHON MATRIX-SIG] bug: assignment from string
>
(snip)


> The other comments are great for the list.
>
> > The problem is with the string in the rhs: m2[1,1] = 'e'
> > (m1[1,1] = 66 works).
> >
> > I suppose the bug is in matrix_ass_sub(), but I can't figure where.
>
> There's actually something rather strange going on here, and while it
> shouldn't dump core, my current interpretation of strings says that this
> should raise an exception.
>>
> Here's what's going on:
>
> 'hello' gets converted to a 5-length vector ['h', 'e', 'l', 'l', 'o']
> 'e' gets converted to a 1-length vector ['e']
> 66 is a 0-dimensional scalar
>
> So, m[1,1] = 'e' is trying to assign a 1-length vector to a 0-dimensional
> scalar, which is an exception.
>
> I can fix this by treating 1-length strings as 0-dimensional scalars if you
> think this is the appropriate choice.

Yes. This is necessary because Python does not have a character type.  1-length
strings must be used as "characters".  Witness the fact that 'hello'[1] wants
to return a character, but returns a string instead.

Jim

=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================