-1

I'm writing a class similar to this one how can I pass values (w,v) to the def q func

class A():
def EM (self,a,b):
self.a=a
self.b=b
w=a+b
print(w)
def number(self,c,d):
self.c=c
self.d=d
v=c-d
print(d)
def q (self,v,w)    ##problrm here 
qq=v+w
print(qq)
  • Does this answer your question? [Python call function within class](https://stackoverflow.com/questions/5615648/python-call-function-within-class) – Daniel Geffen Apr 05 '20 at 14:46

2 Answers2

0

Firstly it helps if you format your code properly as shown:

class A():
    def EM (self,a,b):
        self.a=a
        self.b=b
        w=a+b
        print(w)
     def number(self,c,d):
        self.c=c
        self.d=d
        v=c-d
        print(d)
     def q (self,v,w)    ##problrm here 
        qq=v+w
        print(qq)

But to answer your question you would use the results for EM and number as shown

instance = A()
result = instance.q(instance.EM(a, b), instance.number(c, d))

Because you want to use multiple method results as parameters you have to do it outside of those methods although you could create a new method to make it look better like

class A():
    def EM (self,a,b):
        self.a=a
        self.b=b
        w=a+b
        return w
     def number(self,c,d):
        self.c=c
        self.d=d
        v=c-d
        return d
     def q (self,v,w)
        qq=v+w
        return qq
     def doThisThing(a, b, c, d):
        return self.q(self.EM(a, b), self.number(c, d))

Notice how I've changed the prints to returns as instead of displaying the result to the console we want to pass the result to the caller. If you wanted to now display the result you could use

print(instance.doThisThing(a, b, c, d))
Pyr0lle
  • 80
  • 6
0

Try this out:

a = A()
a.q(w= '''w''',v= '''v''')
JenilDave
  • 606
  • 6
  • 14