odd problem, most likely stupid one too :-)

Hans Nowak wurmy at earthlink.net
Sat Dec 29 02:23:05 EST 2001


Dave Harrison wrote:
> 
> Ive written a class named packet, and I have a list on values that I am using to create my packets by passing the values in as I instantiate the classes I am creating.
> 
> I do the creation in a for loop,
> 
> for item in itemlist:
>         PACKET = Packet(item)
>         paclist.append(PACKET)
> 
> except that when I try to go back through and see the packets in my paclist they are all exactly the same as the final one that I store in the list.
> As in I do,
> 
> for pac in paclist:
>         print pac.getVal()
> 
> and I get exactly the same value, despite the fact that I know for sure they are different.
> 
> If I getVal as I create the packets I get the correct and unique values, but once stored I get the oddity.
> 
> Any ideas ?

Hmm... try printing the id() of every instance in paclist...
if they are the same, then you have somehow added the same
instance over and over again to the list. If not, then they
somehow all ended up with the same value, *or* getValue is
doing something wrong.

Oh yeah, and post the code of the Packet class, that might
give us some clues too. ^_^

--Hans



More information about the Python-list mailing list