odd problem, most likely stupid one too :-)
Tim Roberts
timr at probo.com
Sun Dec 30 17:25:27 EST 2001
Dave Harrison <dlharris at mail.usyd.edu.au> 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.
This might happen if, for example, you were storing the packets in a class
variable or a global variable instead of an instance variable. This class,
for example, would behave exactly as you describe:
class Packet:
value = Null
def __init__(self,pkt):
value = pkt
def getVal(self):
return self.value
The solution is to skip the class global:
class Packet:
def __init__(self,pkt):
self.value = pkt
def getVal(self):
return self.value
--
- Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list
mailing list