[IronPython] ValueError: cannot assign to value types
Curt Hagenlocher
curt at hagenlocher.org
Sat Aug 8 07:04:55 CEST 2009
__setattr__ it's not a *CLR*-level instance method of the type. IronPython
just makes it look like it is.
One possibility is to simply create an entirely new Parcel from scratch,
assigning values as appropriate. For example, given the following C#
classes:
public struct Point {
public int x, y;
}
public class Line {
public Point to, from;
}
You can do this from Python:
F:\tcwb\libs>ipy.exe
IronPython 2.6 Beta 2 (2.6.0.20) on .NET 2.0.50727.4927
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> clr.AddReference('x')
>>> import Point, Line
>>> l = Line()
>>> l.to.x
0
>>> l.to.x = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: cannot assign to value types
>>> l.to = Point(x = 1, y = 2)
>>> l.to.x
1
>>>
On Fri, Aug 7, 2009 at 8:40 PM, Count László de Almásy <calmasy at gmail.com>wrote:
> i'm really stuck trying to change values for an object of type
> 'Parcel', which comes from the openmetaverse library i'm using. how
> this library is implemented i don't have control over, but the given
> interface is to make local changes to these Parcel objects, then use
> its Update() method to commit the changes. but it seems in IronPython
> i can't change these objects.
>
> >>> type(props)
> <type 'Parcel'>
>
> >>> props.Name = "Yes"
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: cannot assign to value types
>
> after hours of struggling, i found
> http://ironpython.codeplex.com/Wiki/View.aspx?title=Value%20Types
> which seems to explain why i'm being bit here. bit it also says
> "updates are still possible via instance methods on the value type
> itself." so isn't "__setattr__" an instance method? seems i'm not able
> to use that either:
>
> >>> props.__setattr__("Name", "Yes")
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: Attempt to update field 'Name' on value type 'Parcel';
> value type fields cannot be directly modified
>
> ok, so what are my options? how do i get around this? i *have* to be
> able to update objects of this type somehow.
>
> i can create a new object of that type, but the constructor doesn't
> allow me to pre-stock it with values ( since i guess it assumes i'll
> be able to change them later), and then of course once it's created i
> can't change anything.
>
> --
> Cheers, László
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090807/3b9f4999/attachment.html>
More information about the Ironpython-users
mailing list