Creating object in function doesn't seem to create a new object.

Matimus mccredie at gmail.com
Tue Jun 3 16:20:14 EDT 2008


> I thought that when I wrote fc1 = FlightCondition() in the function it
> would create a new FlightCondition object which would be passed back
> every time.
> Instead it seems to re-reference the old version and continue to add
> to it.

That is exactly what is happening. You have created a class with two
_class_ attributes that are lists. These attributes are attached to
the _class_ not the instance that was created. You are simply mutating
the list that is attached to the class.

You really want to do something like this:

class FlightCondition(object):
    def __init__(self):
        self.lsf = [0,'Low Speed Flare']
        self.vto = [0,'Vertical Take-Off']

Other than that, its hard to tell what you are actually trying to do,
but likely get_flight_condition could become a method of the
FlightCondition class.

Matt



More information about the Python-list mailing list