Is there a way to set a Python object attribute object.attribute
to return a function or getter method?
For Example:
class CrossSection():
def __init__(self):
self.data = pd.Series([1,3,5,np.nan,6,8])
Now constructing a Panel() Object, I would like to have an attribute Panel.data that returns data from the underlying CrossSection Objects each time (preferably without duplicating the data):
class Panel():
def __init__(self):
# - Core Object - #
self.xs = dict()
self.xs[1] = CrossSection()
self.xs[2] = CrossSection()
# - Data Object - #
self.data = self.find_data()
def find_data(self):
self.data = dict()
for n in self.xs.keys():
self.data[n] = self.xs[n].data
return self.data
In the example above, there are two issues:
- the
find_data()
method runs only on__init__
but if any of the underlying CrossSections() change (or are loaded after__init__
) then self.data wouldn't represent the underlyingCrossSection()
objects. self.data
constructs a new dictionary.
However it does mimic the behaviour I would like:
p = Panel()
p.data
{1: 0 1
1 3
2 5
3 NaN
4 6
5 8
dtype: float64,
2: 0 1
1 3
2 5
3 NaN
4 6
5 8
dtype: float64}
Therefore,
- Is there a way that I can request
Panel.data
such that it updates based on the underlying self.xs dictionary ofCrossSection()
objects. - Can
Panel.data
return a function over the underlyingCrossSection()
objects? [or is my only option to interface using class methods?]