[IronPython] Treat struct as tuple

Steve Baer steve at mcneel.com
Mon Nov 30 19:08:00 CET 2009


Say you have a .NET struct for something like a 3D point
public struct Point3d
{
  private double m_x, m_y, m_z;
  public double X{ get {return m_x;} set {m_x = value;} }
  public double Y{ get {return m_y;} set {m_y = value;} }
  public double Z{ get {return m_z;} set {m_z = value;} }
  // more functions...
}

We have a user that wants to treat this struct like a tuple of 3 values in 
IronPython so he can do things like
pt = Point3d(1,2,3)
for a in pt:
  print a
-or-
pass pt to a function that expects a tuple.

I recommended that the user write a function to coerce the struct to a tuple
def coerceTuple( pt ):
  return [pt.X, pt.Y, pt.Z]

The user said this works, but would like to have something a bit cleaner. Do 
any of you guys suggestions about what I could do to make our Point3d struct 
more "python friendly"?

Thanks,
-Steve

Steve Baer
Robert McNeel & Associates
www.rhino3d.com
www.mcneel.com
 





More information about the Ironpython-users mailing list