0

I write a program in Python. I Have class A. one of its variables,v, is an instance of another class, B:

class A:
 def __init__(self):
   self.v = B()

the class B in in the form of:

class B:
 def __init__(self):
  self.list = [1,2,3]
 def function(self):
  self.list[2] = 1

I create an instance x=A(), put it in a list g (g=[x]) and then change one of the variables in x.v by printing g[0].v.function(). However, when I ask the computer to print g[0].v.list, it prints [1,2,3] rather then [1,2,1]. What can be the reason?

Thank you.

2
  • 1
    Are you sure? The code you posted would do exactly that. Commented Jan 29, 2013 at 18:05
  • Is this the actual code that is having a problem? This code works as described. Commented Jan 29, 2013 at 18:09

1 Answer 1

4

Works for me:

class A:
 def __init__(self):
   self.v = B()

class B:
 def __init__(self):
  self.list = [1,2,3]
 def function(self):
  self.list[2] = 1

x = A()
g = [x]
print g[0].v.function()
print g[0].v.list

output:

None
[1, 2, 1]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.