I'm trying to use Python's @property
functionality with below implementation:
class DataForm:
def __init__(self):
self._curr_dict = dict()
@property
def curr_dict(self):
return self._curr_dict
@curr_dict.setter
def curr_dict(self, key, val):
if 0 < val < 100:
self._curr_dict[key] = val
else:
raise ValueError("Value is not in range")
getter
is working fine, however when I'm trying to set the key/value for curr_dict
, it is throwing below error:
ob1 = DataForm()
ob1.curr_dict
ob1.curr_dict = ('Jack', 10)
error:
TypeError Traceback (most recent call last)
<ipython-input-73-cb86bfc26332> in <module>
----> 1 ob1.curr_dict = ('Jack', 10)
TypeError: curr_dict() missing 1 required positional argument: 'val'
I had read somewhere that setter does not take more than one argument. So how do I implement this? Or, this is not a better approach to solve this problem.
My current implemetation is as below and it is working as expected:
class DataForm:
def __init__(self):
self.data_dict = dict()
def create_key_value_pair(self, key, value):
self.data_dict[key] = value
def get_dict(self):
return self.data_dict