I extended the dictionary class to create my own and now I need to have a method which creates a new blank instance of the class and initialises it with the needed values. The problem I'm facing is that the new instance object is not a new object but just a reference to the self
instance of the class.
Here the class I discussed about:
VALUES = "VALUES"
OPERATORS = "OPERATORS"
class MyDict(dict):
def __init__(self, operators: dict = {}, values: dict = {}) -> None:
super().__init__()
self.set_operators_values(operators, values)
def set_operators_values(self, operators: dict = {}, values: dict = {}):
if not operators.keys() == values.keys():
raise Exception("Keys of operators and values must be the same!!!")
self[OPERATORS]: dict[str, str] = operators
self[VALUES]: dict[str, int] = values
The strange happens when I call the following method:
def extend(self, properties: dict, data_set: DataFrame):
column_types: dict = data_set.dtypes.to_dict()
extended = MyDict()
print("BLANK Extended MyDict:")
print(extended)
# ...
return extended
Here the extended
object is referring to the self
instance.
How can I avoid this and instead create a blank new instance from within its class using a method of the object? Since I also need some data from the self
object I would like to avoid creating a @staticmethod
.
Is there some way to do it?