[Tutor] initializing multiple attributes to same value

Rob McGee i812@iname.com
Thu, 20 Dec 2001 00:14:53 -0600


I tripped myself up on something like this:

{code}
class Whatever:
  def __init__(self):
    self.one = self.two = self.three = 0
    self.listOne = self.listTwo = []

whoKnows = Whatever()

someOtherObject = ['spam', 'eggs']

whoKnows.listOne.append(someOtherObject)
{/code}

With that I also end up with someOtherObject in whoKnows.listTwo.
Presumably if I add 42 to whoKnows.one, I also add 42 to the values for
two and three attributes. Is that right?

So, how do I initialize multiple attributes to the same value? Would
something like this ...

{code}
class Whatever:
  def __init__(self):
    list1 = self.one, self.two, self.three
    for item in list1:
      item = 0
    list2 = self.listOne, self.listTwo
    for item in list2:
      item = []
{/code}

... keep these attributes' values distinct from one another?

Thanks,
    Rob - /dev/rob0