[Tutor] How to replace instances

2008-09-25 Thread Steve Collins
I've written a save/load function for a simple program using cPickle. Upon
saving, a master list, to which all instances are added in their __init__,
is pickled. when the program starts, if the user wishes to load, a variable
"load" is set to one, and the pickled list is loaded. All the classes either
A) initialize using the arguments provided or B) using the the attributes of
the instances in the un-pickled list. This seems a little clunky to me, but
it basically works.
However, some of the instances refer explicitly to other instances
instances. It's obvious why this causes problems. It occurred to me to
simply replace the instances with the ones in the un-pickled list, but I
don't know how.

I tried using the following approach:

class Z:
def __init__(self,y):
self.y = y
def replaceZ (self,withWhat):
self = withWhat


That doesn't raise any errors, but it also doesn't work:

>>> a = X(10)
>>> b = X(20)
>>> print a.y
10
>>> print b.y
20
>>> b.replaceZ(a)
>>> print b.y
20
>>> a
<__main__.X instance at 0x00D4AE18>
>>> b
<__main__.X instance at 0x00D54328>
>>>

Can anyone tell me how to achieve this?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to replace instances

2008-09-25 Thread Steve Collins
On 9/25/08, Kent Johnson <[EMAIL PROTECTED]> wrote:
> On Thu, Sep 25, 2008 at 4:24 AM, Steve Collins <[EMAIL PROTECTED]>
> wrote:
>
>> However, some of the instances refer explicitly to other instances
>> instances. It's obvious why this causes problems. It occurred to me to
>> simply replace the instances with the ones in the un-pickled list, but I
>> don't know how.
>
>> I tried using the following approach:
>>
>> class Z:
>> def __init__(self,y):
>> self.y = y
>> def replaceZ (self,withWhat):
>> self = withWhat
>
> 'self' is just another parameter passed to the method, so this just
> rebinds a local name.
>

That makes sense. At least I understand why it doesn't work now.

> This example doesn't seem to illustrate the situation you describe.
> You can replace the 'y' attribute of a Z object by assigning to it.
>
> One solution might be to make all your objects pickleable. Pickle
> tracks object references and handles embedded references correctly.
>
>> That doesn't raise any errors, but it also doesn't work:
>>
>>>>> a = X(10)
>>>>> b = X(20)
>
> Presumably this should be Z(10), Z(20) ?
>>>>> print a.y
>> 10
>>>>> print b.y
>> 20
>>>>> b.replaceZ(a)
>>>>> print b.y
>> 20
>
> If you want 'b' to refer to the same thing as 'a', just assign
> b = a

that's what I was trying (incorrectly) to achieve the above example.
but how can I do this for an arbitrary number of objects in a list?

I have a list x = [a,b,c] and a list y = [d,e,f], both filled with
instance objects. I want the instance at x[n] to reference the
instance at y[n]

n = 0
while n < range(len(x))
x[n] = y[n]
n+=1

given the contents of your link, I understand why x == [d,e,f] and not
a == d etc., but how can I make a == d by iterating through two lists?

>
> I think you have some common misconceptions about the nature of
> variables and assignment in Python. This may help:
> http://personalpages.tds.net/~kent37/kk/00012.html
>
> Kent
>
I should note that I am very much a novice programmer, and really
appreciate the feedback.

Thanks,
Steve
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor