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

Discussion in 'Python' started by Paul Childs, Jun 3, 2008.

  1. Paul Childs

    Paul Childs Guest

    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_condition(flight_data):

    fc1 = FlightCondition()

    for row in flight_data:

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


    print 'in function get_flight_condition'
    print fc1.lsf
    print fc1.vto

    return fc1

    for count in range(3):

    fc = get_flight_condition([1,2,3])

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

    When I run it I get...

    in function get_flight_condition
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']
    returned fc
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']
    in function get_flight_condition
    [6, 'Low Speed Flare']
    [6, 'Vertical Take-Off']
    returned fc
    [6, 'Low Speed Flare']
    [6, 'Vertical Take-Off']
    in function get_flight_condition
    [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_condition
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']
    returned fc
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']
    in function get_flight_condition
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']
    returned fc
    [3, 'Low Speed Flare']
    [3, 'Vertical Take-Off']
    in function get_flight_condition
    [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
     
    Paul Childs, Jun 3, 2008
    #1
    1. Advertising

  2. Paul Childs

    Matimus Guest

    > 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
     
    Matimus, Jun 3, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Axel Dahmen

    Creating New Project Doesn't Create Web App

    Axel Dahmen, Nov 3, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    318
    Axel Dahmen
    Nov 3, 2003
  2. Bill Green

      in a ListItem doesn't seem to work

    Bill Green, Feb 6, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    440
    vMike
    Feb 7, 2004
  3. Mike Dinnis

    WSDL.exe doesn't seem to create the proxy class

    Mike Dinnis, Oct 28, 2003, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    243
    Mike Dinnis
    Nov 3, 2003
  4. Eric
    Replies:
    8
    Views:
    126
    Evertjan.
    Oct 1, 2006
  5. dphizler
    Replies:
    1
    Views:
    89
    dphizler
    Apr 14, 2010
Loading...

Share This Page