0

I've a problem with Python. I've this main:

def main():
    cats = []
    for x in range(1, 11):
        x = Cat(x)
        cats.append(x)  


    listPussa = []
    for x in range(1,21):
       x = Pussa(x)
       listPussa.append(x)


    PussaIndex = 0
    for cat in cats:
        num = cat.getcatNum()

        if num % 2 != 0:
            for i in range(4):
                cat.addPussa(listPussa[PussaIndex])
                PussaIndex += 1

    for cat in cats:
      print cat.mixeta()

The problem is that when I use this function in the main, to print the list of cats:

def mixeta(self): #This is a method of the class Cat, it returns a few strings depending on how many objects Pussa has the object. 
    if len(self.__listPussa) is 0:  #When the cat has no one, it returns that
        return "Miau!", self.getcatNum(), "La llista de pusses esta buida" #The string menas that the list of Pussa is empty.

    else: #When it has at least 1
        listIdPussa = [] #I created a new list 
        for pussa in self.__listPussa: #And for every objcet Pussa in the list of Pussa
            listIdPussa.append(pussa.getIdPussa()) #I save every idPussa in the list.

        return  "Miau!", self.getcatNum(), "El gat esta infectat. El nombre total de pusses es ", len(self.__listPussa), listIdPussa #So, at the end it returns "Meow", the number of the cat, and it says that the cat is "infected" and the total number of objects Pussa is X and each id from every object

The result it has to be that:

('Miau!', 1, 'El gat esta infectat. El nombre total de pusses es ', 4[0,1,2,3])

('Miau!', 2, 'La llista esta buida') # It means the list has no objects Pussa

('Miau!', 3, 'El gat esta infectat. El nombre total de pusses es ', 4[4,5,6,7])

('Miau!', 4, 'La llista esta buida')

And this way until it reach Miau, 10.

But the problem is that my main prints that:

('Miau!', 1, 'El gat esta infectat. El nombre total de pusses es ', 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

('Miau!', 2, 'El gat esta infectat. El nombre total de pusses es ', 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

('Miau!', 3, 'El gat esta infectat. El nombre total de pusses es ', 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

('Miau!', 4, 'El gat esta infectat. El nombre total de pusses es ', 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

And that for all cats. What can I do?

Valentin Lorentz
  • 9,556
  • 6
  • 47
  • 69
Atsuko
  • 11
  • 5
  • is that a dialect of Spanish? or Catalan? – Erik Kaplun Mar 06 '14 at 17:22
  • And what should it do instead? – tobias_k Mar 06 '14 at 17:22
  • Yup, It's Catalan! The first result is what it should do. – Atsuko Mar 06 '14 at 17:23
  • cool; just FYI—it's harder to understand what your code does if it's got some words people don't understand, such as "mixeta"... especially if you haven't explained/commented what it does. – Erik Kaplun Mar 06 '14 at 17:24
  • Okay! I'll edit it ^^ Sorry – Atsuko Mar 06 '14 at 17:25
  • by the way, the 2 list initializations could be refactored like this `cats = [Cat(x) for x in range(1, 21)]` (...also, reading [PEP8](http://www.python.org/dev/peps/pep-0008/) is strongly recommended :)) – Erik Kaplun Mar 06 '14 at 17:26
  • Okay, thank you! I've edited the code ^^ I hope it I'll be easier now. – Atsuko Mar 06 '14 at 17:28
  • 5
    As you seem to have taken the code from my answer on [this other question](http://stackoverflow.com/questions/22132094/classes-and-methods-with-lists-in-python), would you mind [accepting that one](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) to make sure that question is resolved? – poke Mar 06 '14 at 17:29
  • Yes sure! Sorry I'm new in this site > – Atsuko Mar 06 '14 at 17:31
  • 1
    @Atsuko No worries, we all were new to it at some point, and thanks :) – poke Mar 06 '14 at 17:33
  • I need help >< No one knows how to do it? – Atsuko Mar 06 '14 at 17:51

1 Answers1

1

This probably happens because you declared __listPussa as a class member and not as object member. In this case, it is shared between all the instances of Cat.

class Cat:
    __listPussa = []

    def __init__(self, num):
        self.num = num

Each time you add some Pussa to cats, their are added to the same variable shared accross all cats (yeek, cats share their Pussa... :) )

To achieve what you want, you need to make __listPussa an instance member, like this:

class Cat:

    def __init__(self, num):
        self.num = num
        self.__listPussa = []
Cilyan
  • 7,883
  • 1
  • 29
  • 37
  • I had this too! `code`def __init__(self, catNum = None, listPussa = []): self.__catNum = catNum self.__listPussa = listPussa – Atsuko Mar 06 '14 at 18:08
  • @Atsuko Unfortunately, using `[]` as a default argument won’t really work (see [this question](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument)). Set it to `[]` within the function itself instead. – poke Mar 06 '14 at 18:24
  • THANK YOU! There was the error, in the class __init__!! Thanks a lot really! – Atsuko Mar 06 '14 at 18:38