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

P

Paul Childs

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
 
M

Matimus

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top