Trying to append items to a list in python but its acting odd
Question: I have events, people can belong an event. So I have an event class that looks
like:

    
    
    class Event():
        name = ""
        people = []
    

I also have a global variable to hold all the events

    
    
    events   = []
    

Now I'm trying to process my raw data into events and people going to them:

    
    
        # If there are missions, add the events
        for m in wiki.missions:
            foundEvent = False
            for e in events:
                if e.name == m:
                    foundEvent = True
                    foundPerson = False
                    for p in e.people:
                        if p.rawName == person.rawName:
                            foundPerson = True
                    if not foundPerson:
                        e.people.append(person)
                        print "Added " + person.display + " to " + m + " (" + str(len(e.people)) + ")"
    
            if foundEvent == False:
                event = Event()
                event.name = m
                event.people.append(person)
                print "New " + person.display + " to " + m + " (" + str(len(event.people)) + ")"
                events.append(event)
                event = None
    

Based on my print statements I can see who is added to existing event and when
and who makes a new event. The odd thing is the count of people in an event is
always increments the people in all events.

    
    
    New Joseph M. Acaba to STS-119 (1)
    New Joseph M. Acaba to Soyuz TMA-04M (2)
    New Joseph M. Acaba to Expedition 31 Expedition 32 (3)
    Added Dominic A. Antonelli to STS-119 (4)
    New Dominic A. Antonelli to STS-132 (5)
    Added Richard R. Arnold to STS-119 (6)
    

This makes no sense to me, what is it than I am doing wrong? (I'm sure there
is a lot)


Answer: You should be using instance variables instead of class variables. So set up
your class something like this, and modify the rest of your code to fit it:

    
    
    class Event:
        def __init__(self, name):
            self.name = name
            self.people = []
    

The important difference here is just as you described: instance variables are
set for a specific instance of the class, whereas class variables are set for
_all_ instances of that class.

