![]() |
Using one method's result for another method in the same Class? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Using one method's result for another method in the same Class? (/thread-29621.html) |
Using one method's result for another method in the same Class? - btownboy - Sep-12-2020 Hey Guys, I have a question as mentioned in the title. can you use one method's result for another method in the same Class?. I wish to use the result of the area method in the get_amount_inside method. Is there a way to do this? or put the equation for area L*W. Thanks for your Help! class Rectangle: def __init__(self, width, height): self.width = width self.height = height def get_area(self): area = self.width * self.height return area def get_amount_inside(self, shape): self.shape = shape if self.shape == "sq": num_times = self.area/(self.width * self.width) return num_times RE: Using one method's result for another method in the same Class? - bowlofred - Sep-12-2020 Sure. Normally you'll just use self to call the method. Something like: def get_amount_inside(self): area = self.get_area() # do more stuff using the area.... get_area() is a method (function), not an attribute. So you need the parenthesis to call it.
RE: Using one method's result for another method in the same Class? - btownboy - Sep-13-2020 Thanks buddy! appreciate it. RE: Using one method's result for another method in the same Class? - buran - Sep-13-2020 Note that you can turn get_area into property using @property decorator. class Rectangle: def __init__(self, width, height): self.width = width self.height = height @property def area(self): return self.width * self.height rect = Rectangle(10, 2) print(rect.area) |