473,378 Members | 1,388 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,378 software developers and data experts.

object creation

for record in roll:
x = box()
x.createSomething(record)
do something ....
Can anyone tell me why python keeps return the original object x that
was created in the FOR loop. I want to instantiate a new x object for
each iteration of the FOR loop

Nov 14 '08 #1
6 1290
On Nov 14, 5:16*pm, BiraRai <bira...@gmail.comwrote:
for record in roll:
* * x = box()
* * x.createSomething(record)
* * do something ....

Can anyone tell me why python keeps return the original object x that
was created in the FOR loop. *I want to instantiate a new x object for
each iteration of the FOR loop
What is box()? Pasting its definition would help.

George
Nov 14 '08 #2
On Nov 14, 5:44*pm, George Sakkis <george.sak...@gmail.comwrote:
On Nov 14, 5:16*pm, BiraRai <bira...@gmail.comwrote:
for record in roll:
* * x = box()
* * x.createSomething(record)
* * do something ....
Can anyone tell me why python keeps return the original object x that
was created in the FOR loop. *I want to instantiate a new x object for
each iteration of the FOR loop

What is box()? Pasting its definition would help.

George
class box:
a = int()
b = int()

def createSomething(self,x):
Nov 14 '08 #3
On Fri, Nov 14, 2008 at 6:10 PM, BiraRai <bi*****@gmail.comwrote:
class box:
a = int()
b = int()

def createSomething(self,x):
At a guess, x = box() does create a new instance of your box class,
but since you've declared a and b to be class variables instead of
instance variables, it doesn't look that way to you.

Try adding the line:
print id(x)
after you call x = box(), and you should see that.

Then add
def __init__(self):
a = 0
b = 0

to your box class to make a and b instance variables.

--
Jerry
Nov 14 '08 #4
On Nov 14, 2008, at 4:33 PM, Jerry Hill wrote:
Then add
def __init__(self):
a = 0
b = 0

to your box class to make a and b instance variables.
Doesn't that have to be "self.a" and "self.b"?

Best,
- Joe

Nov 14 '08 #5
On Fri, 14 Nov 2008 16:38:15 -0700, Joe Strout wrote:
On Nov 14, 2008, at 4:33 PM, Jerry Hill wrote:
>Then add
def __init__(self):
a = 0
b = 0

to your box class to make a and b instance variables.

Doesn't that have to be "self.a" and "self.b"?

Only if you want it to work :)


--
Steven
Nov 15 '08 #6
On Fri, Nov 14, 2008 at 6:38 PM, Joe Strout <jo*@strout.netwrote:
On Nov 14, 2008, at 4:33 PM, Jerry Hill wrote:
>Then add
def __init__(self):
a = 0
b = 0

Doesn't that have to be "self.a" and "self.b"?
Yes, that should indeed have been self.a and self.b! Sorry about that.

--
Jerry
Nov 15 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Richard | last post by:
Hi all, I am looking for some help on understanding the overhead associated with object creation in Java. I am writing an application where I have written a class to encapsulate some text...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
3
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
8
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
3
by: Nick Dreyer | last post by:
I was quite surprised to notice that Sub New() gets called twice, once at declaration time and once at creation time. I can't figure out why it would be called at declaration if there is no class...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
by: Dhananjay | last post by:
Hi All, I want to develop one application in vb.net for exchange 2000. I tried to add one contact with the code snippet below. The same logic is there for appointment on Microsoft's site. (I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.