[IronPython] Immutable value types, again

drew moore drew at astro.pas.rochester.edu
Sun May 6 23:04:50 CEST 2007

Hi guys!

I recently wrote some (pure CPython!) code that allows the user to get 
and set a vector (3d point) via a property that wraps an inner vector 
value with a coordinate transformation. For the most part, it does the 
right thing, but client code like:

model.point.X = 3.5

does not do the right thing. The point property returns a brand new 
vector, its X attribute is modified, and then the whole thing is garbage 
collected. Oops, but no big deal. Once you remember that it works that 
way, you code appropriately. It makes sense when you understand what's 

I also recently tried to use a (proprietary) 3D library with IronPython. 
While I was able to see the vector structure that was exposed in the 
tlbimp.exe - created .NET assembly (kudos to IronPython -- this same 
struct is invisible to CPython's win32com, since it has no guid) I could 
not find a way to actually modify the members of this struct. I had to 
write some C# to get the job done. Hey, we're all consenting adults 
here... Where the heck is the object.__setattr__ way to change one of 
these "mostly immutable" .NET value types? I couldn't find it.

Well, I found a way to add a guid and get the COM stuff working, and 
when I got my CPython code connected to the same struct, it was mutable. 
Yay! Of course, it was a copy, so the same misbehavior could happen in a 
containment situation. The thing is, I found "possibly making a coding 
mistake" preferable to "having something be impossible to do in 
IronPython when it's possible in all the other .NET languages". (I 
*rarely* find myself needing to explain why something is impossible to 
do in Python! WTF??) This was my first honest attempt to do something 
"real" with IronPython. First impressions, guys... Of course, I might be 
missing something too, so clue me in if I am doing something stupid..


I'm not sure that the "draconian" immutable value types decision, 
although well-intentioned, was the Pythonic thing to do. It prevents you 
from making a coding mistake that you can make very easily in pure 
CPython, and it prevents a whole lot more as well. Am I the only one, or 
has anyone else bumped into this one? Is there a "mutable value types" 
switch that I don't know about? Is there another back door into these 
objects, or do I need to wait for ctypes in IronPython?




More information about the Ironpython-users mailing list