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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Childs

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

    Hi folks,

    I'll start off with the code I wrote...
    (ActivePython 2.4 on Windows XP SP2)

    -------------------------------
    class FlightCondition (object):

    lsf = [0,'Low Speed Flare']
    vto = [0,'Vertical Take-Off']

    def get_flight_cond ition(flight_da ta):

    fc1 = FlightCondition ()

    for row in flight_data:

    fc1.lsf[0] += 1
    fc1.vto[0] += 1


    print 'in function get_flight_cond ition'
    print fc1.lsf
    print fc1.vto

    return fc1

    for count in range(3):

    fc = get_flight_cond ition([1,2,3])

    print 'returned fc'
    print fc.lsf
    print fc.vto
    ---------------------------------

    When I run it I get...

    in function get_flight_cond ition
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']
    returned fc
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']
    in function get_flight_cond ition
    [6, 'Low Speed Flare']
    [6, 'Vertical Take-Off']
    returned fc
    [6, 'Low Speed Flare']
    [6, 'Vertical Take-Off']
    in function get_flight_cond ition
    [9, 'Low Speed Flare']
    [9, 'Vertical Take-Off']
    returned fc
    [9, 'Low Speed Flare']
    [9, 'Vertical Take-Off']

    ---------------------------------
    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.

    ---------------------------------
    What I expected was...

    in function get_flight_cond ition
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']
    returned fc
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']
    in function get_flight_cond ition
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']
    returned fc
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']
    in function get_flight_cond ition
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']
    returned fc
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']

    ---------------------------------

    Could someone please explain to me why I get the output I did instead
    of what I expected.

    How do I code my function so I get a new fc1 every time and my desired
    output?

    Thanks in advance.

    /Paul
  • Matimus

    #2
    Re: Creating object in function doesn't seem to create a new object.

    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_cond ition could become a method of the
    FlightCondition class.

    Matt

    Comment

    Working...