473,288 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

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_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
1 1039
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.