[IronPython] Is this a bug in IP 2.0?

Michael Foord fuzzyman at voidspace.org.uk
Tue Dec 23 22:01:21 CET 2008


Mark Russo wrote:
> In IronPython 2.0, when I replace a Point object in a list using 
> assignment, a copy of the Point object is used to replace the original 
> list element rather than the original Point itself. This does not 
> happen with user-defined objects. It also does not happen with Point 
> objects in IronPython 1.1.

Point objects are value types, so some operations cause them to be 
copied. Probably the way they are used under the hood has changed with 
IronPython 2.

Michael

>
> IronPython 2.0 (2.0.0.0) on .NET 2.0.50727.3053
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import clr
> >>> clr.AddReference("System.Drawing")
> >>> from System.Drawing import *
> >>>
> >>> class C(object): pass
> ...
> >>> def test1():
> ...     p1 = C()                # Create two objects
> ...     p2 = C()
> ...     l = [p1]                # Put one in a list
> ...     print id(l[-1]), id(p2) # Print ids of object in list (p1) and p2
> ...     l[-1] = p2              # Replace p1 with p2 in list
> ...     print id(l[-1]), id(p2) # Print ids of object in list and p2
> ...
> >>> def test2():
> ...     p1 = Point(1,2)         # Create two Points
> ...     p2 = Point(3,4)
> ...     l = [p1]                # Put one in a list
> ...     print id(l[-1]), id(p2) # Print ids of Point in list (p1) and p2
> ...     l[-1] = p2              # Replace p1 with p2 in list
> ...     print id(l[-1]), id(p2) # Print ids of Point in list and p2
> ...
> >>> test1()
> 43 44
> 44 44
> >>> test2()
> 45 46
> 47 46   # The id of the Point before and after adding to the list is 
> different
> >>>
>
> Here is the same program in IronPython 1.1
>
> IronPython 1.1 (1.1) on .NET 2.0.50727.3053
> Copyright (c) Microsoft Corporation. All rights reserved.
>
> <snip>
>
> >>> test1()
> 43 44
> 44 44
> >>> test2()
> 45 46
> 46 46   # The id of the Point before and after adding to the list is 
> the same
> >>>
>
> ... or am I missing something.
>
>
> ------------------------------------------------------------------------
> Send e-mail faster without improving your typing skills. Get your 
> Hotmail® account. 
> <http://windowslive.com/online/hotmail?ocid=TXT_TAGLM_WL_hotmail_acq_speed_122008> 
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog





More information about the Ironpython-users mailing list