[IronPython] Array Access Problem

Jim Hugunin jimhug at exchange.microsoft.com
Thu May 5 20:42:40 CEST 2005

This is a known problem, but we don't have a good fix to it in IronPython.  Here's how to set just the X value of a Point in an array today:

apt[0] = Point(0, apt[0].Y)


p = apt[0]
p.X = 42
apt[0] = p

I suspect that you're not very excited by either of these options.  The problem is that Point is a value type/struct and thus needs to be boxed to be passed out to IronPython as an object.  When it is boxed a new copy of the data in the Point is created and changes to this new copy won't change the original back in the array.

This same problem also shows up when value types are stored in fields and you want to change them.  Point is probably far and away the most common instance of this.

I can't come up with any elegant ways to make this experience better.  There are some inelegant ideas floating around in my head, but I keep hoping for an inspiration here...

In case you're wondering why it's hard, Python has a strong invariant that:

tmp = a[index]

My most elegant solution would be to break this invariant in IronPython when setting value types.  This would add some additional complexity to the compiler, but not a huge amount.  However, any time that I think about changing an invariant of the Python language I get rather nervous about the ramifications...

Thanks - Jim
From: users-ironpython.com-bounces at lists.ironpython.com [mailto:users-ironpython.com-bounces at lists.ironpython.com] On Behalf Of Kirk Olynyk
Sent: Thursday, May 05, 2005 9:56 AM
To: users-ironpython.com at lists.ironpython.com
Subject: [IronPython] Array Access Problem

I can't seem to change the members of an array. Is this a known problem?
import sys
from System import*
from System.Drawing import *
apt = Array.CreateInstance(Point, 1)
apt[0] = Point(1,2)
print apt[0]
apt[0].X = 0    # change the x-coordinate
print apt[0]    # check to see if it set

More information about the Ironpython-users mailing list