问题
class MyClassA(object):
def __init__(self):
entry = input("Insert a value ::: ")
b = MyClassB(entry) #To pass the variable entry to class MyClassB
c = MyClassC() #Initializied MyClassC to be ready for receive the value p
self.x = b.f #To get back the value f from MyClassB
print(self.x)
self.x1 = c.p #To get back the value f from MyClassC
print(self.x1)
class MyClassB(object):
def __init__(self, M):
self.f = M * 10 # f will contain (the value entry from MyClassA *10)
c = MyClassC(self.f) #To pass variable f to class MyClassC
class MyClassC(object):
def __init__(self,passedVar):
self.p = passedVar + 0.1 # p will contain (the value entry from MyClassB + 0.1)
h = MyClassA()
Above is my sample code, first of all it will ask for a value and store it into variable named entry.
Then the second class named MyClassB will recover the value inside entry, after processing the entry value, a new one will be stored into new variable named f which will be passed to third class named MyClassC then also back to first class.
Finally the third class does some processing of the f value and stores it into yet another variable p.
That's how my code should work when the user runs the program, and this is what it would look like:
Insert a value ::: 9 (assume the user typed 9 here)
so the output should be:
90
90.1
Unfortunately my problem is that passing the value from MyClassC to myClassA doesn't work.
It says I must pass it two argument from the first class but it'a going to get the value after executing the 2st class.
I think that I have problem with initializing instances, could someone correct my code to work the way I described?
回答1:
The problem you're having is because there are two independent instances of MyClassC being created, one in MyClassA.__init__() and a separate one in MyClassB.__init__().
The easy way to fix it — not necessarily the best — would be to make MyClassB.__init__() store the MyClassC instance it creates in yet another instance attribute, and then refer to the attribute of that object when you want to retrieve the value of p.
Here's what I mean:
class MyClassA(object):
def __init__(self):
entry = input("Insert a value ::: ")
b = MyClassB(entry) # To pass the variable entry to class MyClassB
####### c = MyClassC() # Initializied MyClassC to be ready for receive the value p
self.x = b.f # To get back the value f from MyClassB
print(self.x)
self.x1 = b.c.p # To get back the value p from MyClassC instance created in MyClassB
print(self.x1)
class MyClassB(object):
def __init__(self, M):
self.f = M * 10 # f will contain (the value entry from MyClassA *10)
self.c = MyClassC(self.f) # Pass variable f to class MyClassC and save instance
class MyClassC(object):
def __init__(self, passedVar):
self.p = passedVar + 0.1 # p will contain (the value entry from MyClassB + 0.1)
h = MyClassA()
回答2:
In line
c = MyClassC()
it should be
c = MyClassC(b.f)
回答3:
Or you could set the value p to the class MyClassC
class MyClassC(object):
def __init__(self, passedVar):
MyClassC.p = passedVar + 0.1
but keep in mind that this situation can happen
class T(object):
def __init__(self, x):
T.x = x
if __name__ == '__main__':
t1 = T(3)
print t1.x, T.x
t1.x = 1
print t1.x, T.x
t2 = T(2)
print t1.x, t2.x, T.x
# output:
3 3
1 3
1 2 2
来源:https://stackoverflow.com/questions/36575062/passing-values-between-class-and-instance