python - Accessing wrong attribute when trying to reset value...? -


in example code i'm trying make whole pile of data callable:

class data(object):     def __init__(self, data):         self._raw_data = data          key, val in data.items():             if isinstance(val, dict):                 if 'data' in val.keys():                     setattr(self, key, dataset(val))                 else:                     setattr(self, key, data(val))             else:                 setattr(self, key, val)      def __getattr__(self, name):         return none      @property     def raw(self):         return self._raw_data   class dataset(data):     def __init__(self, data):         self._raw_data = data         super().__init__(data)         entry in self.data:             # need debug             self.data[self.data.index(entry)] = data(entry) 

what ends happening this. i'll try:

>>> example = {'dataset': {'id': 1, 'data': [{'param1': 3, 'param2': 4}, {'param1': 5, 'param2': 6}]}} >>> data1 = data(example) >>> print(data1.dataset.id) 1 

now works expected. problem last line, causes change in __raw_data attribute in dataset object , have no idea how (and also, looks wrong). don't understand important inherritance. trying put data callable attributes, keep raw data kinds of purposes. however:

>>> print(data1.raw) {'dataset': {'id': 1, 'data': [<wtf.data object @ 0x102a8da58, <wtf.data object @ 0x102a8da90>]}} 

it should return raw data passed:

>>> print(data1.raw) {'dataset': {'id': 1, 'data': [{'param1': 3, 'param2': 4}, {'param1': 5, 'param2': 6}]}} 

it works data objects:

>>> item in data1.dataset.data: ...     print(item.raw) {'param2': 4, 'param1': 3} {'param2': 6, 'param1': 5} 

thanks help.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -