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

object creation

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.