By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.