I'm trying to create a custom class for dimensionality that I can basically use as an integer but also add metadata and units to in the process.
I am trying to follow the class inheritance tutorial: https://www.python-course.eu/python3_inheritance.php but it doesn't like my extra attributes.
How can I create this class?
I want to be able to do the following:
m = Dimension(4, "iris_feature")
m + 1 # 5
np.random.normal(size=(150,m))
m.metadata["notes"] = "Darth Plagueis still lives"
print(f"This dimension's units are {m.unit}s") # This dimension's units are iris_features
Here is my attempt to make the class:
class Dimension(int):
def __init__(self, size, unit=None, metadata=None):
self.size = size
self.unit = unit
self.metadata = dict() if metadata is None else metadata
def __repr__(self):
return f"Dimension(size: {self.size}, unit: {self.unit})"
n = Dimension(150, "iris_sample")
m = Dimension(4, "iris_feature")
Here is my error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-e0983ebb366a> in <module>()
7 return f"Dimension(size: {self.size}, unit: {self.unit})"
8
----> 9 n = Dimension(150, "iris_sample")
TypeError: 'str' object cannot be interpreted as an integer