Dear Sage team,

i guess the following is more a python- than a Sage-problem, and also
i have a work-around. But perhaps you can help me to learn something.

I have the following code in a module MyTest.so:

class T:
    def __init__(self,D={}):
        self.Data = D
    def __repr__(self):
        return str(self.Data)
    def __setitem__(self,I,v):
        self.Data[I] = v

Now, creating two instances of T and changing one of them changes the
other, too:

sage: from MyTest import T
sage: a=T()
sage: a[(4,3)]=2
sage: b=T()
sage: b
{(4, 3): 2}
sage: a[(3,4)]=5
sage: b
{(3, 4): 5, (4, 3): 2}

But why? Both in a and b, the Data-attribute should be initialized as
{}. Moreover, a.Data and b.Data should be different objects, because
"D={}" should create a *new* empty dictionary each time. So, i don't
see what goes wrong.

The work-around is:

    def __init__(self,D={}):
        self.Data = copy(D)

However, i don't understand why copy() is needed here.

Thanks in advance for an explanation
      Simon


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to