Very often my classes have a few attributes, and other object's properties depend on those. What is the best way to define and access those? A minimal example should explain what I'm asking.
Say that I have a class that defines a circle:
class Circle:
def __init__(self, r):
self.r = r
Now this circle is instantiated just based on the radius, but I may want to be able to access its diameter, these are the possibilities I see:
A) create a new attribute when instantiating
class Circle:
def __init__(self, r):
self.r = r
self.d = 2*r
B) define a function that returns the diameter
class Circle:
def __init__(self, r):
self.r = r
def d(self):
d = 2*r
return d
Solution A does not ensure consistency, if the user changes r, d is not automatically updated, and the mistake can be hard to track down. Solution B requires the user to call a function rather than accessing an attribute. And every time the attribute d has to be accessed, it has to be recalculated, and in general it might be time consuming.
Is there a solution C that ensures consistency and does not require the class user to recalculate the attribute every time it is accessed?