0

I only get one output that is multiply:6 but I want both outputs.

Why am I not getting a return value if I used return in class Add

class Add:
    def result(self, x, y):
        return f"add, a, b"

class multi(Add):
    def result(self, a, b)
        p=a*b
        super().result (1, 2)
        return f"multiply:{p}"


x=multi() 
print(x.result(2, 3)) 

I want 2 outputs together

add: 3
multyply:6
1
  • when i put print(f"add: {q}) instead of return(f"add: {q}) I get out out as i want... But i want to return not print. Commented Oct 15, 2022 at 6:49

3 Answers 3

1

you can return and print where you are calling super().result

class Add:
  def result(self, x, y):
    return f"add => {x} + {y} = {x+y}"

class multi(Add):
  def result(self, a, b):
    print(super().result (1, 2))
    return f"multyply => {a} * {b} = {a*b}"


x=multi() 
print(x.result(2, 3))
Sign up to request clarification or add additional context in comments.

2 Comments

thank you bhai.. But what if i don't add print(super(). result(1, 2)). I don't want to add print i want to return.
If you don't add print then returned thing will not be used anywhere and thatswhy you are not getting anything. As you can see when you are returning from multi then also you are printing that else it will not be printed.
1

If you don't want to print inside the function multi:

class Add:
    def result(self, x, y):
        return f"add:{x+y}"


class multi(Add):
    def result(self, a, b):
        p=a*b
        add = super().result (1, 2)
        return add+"\n"+f"multiply:{p}"


x=multi() 
print(x.result(2, 3)) 

Comments

0

Correct way:

class Add:
    def result(self, x, y):
        return f"add:{x + y}"

class Multi(Add):
    def result(self, a, b):
        add_result = super().result(a, b)
        p = a * b
        return f"{add_result}\nmultiply:{p}"

x = Multi()
print(x.result(2, 3))

Output:

add:5
multiply:6

Explanation:

You're calling the parent method (super().result()), but not capturing or printing its return value. In Python, when you override a method, the parent method doesn’t automatically run unless you explicitly call it — and even then, you need to use its return value (either store it or print it).

Hope that clears it up!

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.