The short answer has already been posted. Use an instance variable (self.answer):
class QuadEq(object):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def solve_quad_eq(self):
self.answer = ("this", "that")
return self.answer
def show_result(self):
print self.answer
eq = QuadEq(1,2,3)
eq.solve_quad_eq()
eq.show_result()
Loosely speaking, an instance variable (data member) is just a variable whom lifespan is the same as the one of its "owner" (in the example, the object referenced by eq).
And now, for the long -- and slightly more pedantic - answer: when designing a class, you have to think about its responsibilities and its state. Simply said, what is the purpose of your class? Is it just a container for various more-or-less related functions? In that case, the above answer is perfectly acceptable. But usually, you have to be a little bit more rigorous -- at the very least in order to improve understandability/maintainability of your code.
Here you have a QuadEq class. By its name, I understand an instance of this class models one equation. Since the roots of such an equation are a properties of that equation, I think it is acceptable to have the method solve_quad_eq to be a method of that class. With the slight change I would use the more generic name solve. Why? Because that make provision for future classes for different equations providing the same semantic. In addition, both returning the result and storing it in an instance variable might be confusing. You should make a choice here. Not mentioning the fact your function sometimes returns the roots, other time the number of roots (0).
Now, printing. I am more skeptical about this one. Displaying itself is not a "native" property of an equation. And if you go that way, you'll soon have to deal in your(s) equation class(es) with problems totally unrelated with "equations": how to write in a file? Binary or text? Which encoding should I use? How to deal with I/O errors? and so on...
So, if I were you, I would push toward separation of concern, providing just an "accessor" method to return the roots -- and display them from the outside. Since this seems to be important, I keep here the separation between that accessor and the solve method (potentially computationally intensive for some kinds of equations). Using the instance variable self.answer merely as a cache (memoization)
Here is a full example:
class Eq(object):
def __init__(self):
self.answer = None # this should be calles "roots", no?
def roots(self):
if self.answer is None:
self.solve()
return self.answer
class QuadEq(Eq):
def __init__(self, a, b, c):
Eq.__init__(self)
self.a = a
self.b = b
self.c = c
def solve(self):
self.answer = ("this", "that")
return 2
eq = QuadEq(1,2,3)
print(eq.roots())
Please note how easy it is now to add an other kind of equation to solve in the program ...
class OtherEq(Eq):
def __init__(self, a, b, c):
Eq.__init__(self)
self.a = a
self.b = b
self.c = c
def solve(self):
self.answer = ( "it", )
return 1
... and more important, the code to use that new kind of equation is almost the same as the previous one:
eq = OtherEq(1,2,3)
print(eq.roots())