Posts

Showing posts with the label python-dataclasses

Python 3.7: Utility of Dataclasses and SimpleNameSpace

Python 3.7: Utility of Dataclasses and SimpleNameSpace Python 3.7 provides new dataclasses which have predefined special functions. dataclasses From an overview point, dataclasses and SimpleNameSpace both provide nice data encapsulating facility. dataclasses SimpleNameSpace @dataclass class MyData: name:str age: int data_1 = MyData(name = 'JohnDoe' , age = 23) data_2 = SimpleNameSpace(name = 'JohnDoe' , age = 23) A lot of times I use SimpleNameSpace just to wrap data and move it around. SimpleNameSpace I even subclass it to add special functions: from types import SimpleNameSpace class NewSimpleNameSpace(SimpleNameSpace): def __hash__(self): return some_hashing_func(self.__dict__) For my question: SimpleNameSpace dataclasses SimpleNameSpace dataclasses SimpleNamespace only adds the attributes and a __repr__ . dataclass adds much more, e.g. __eq__ , __hash__ , .. – L3viathan Jun 28 at...