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
__repr__
__eq__
__hash__
2 Answers
2
The short answer is that this is all covered by PEP 557. Taking your questions slightly out of order...
The PEP is quite clear that they are not a replacement and expect the other solutions to have their own place.
Like any other design decision, you'll therefore need to decide exactly what features you care about. If that includes the following, you definitely don't want dataclasses.
Where is it not appropriate to use Data Classes?
API compatibility with tuples or dicts is required.
Type validation beyond that provided by PEPs 484 and 526 is required, or value validation or conversion is required.
That said, the same is true for SimpleNameSpace, so what else can we look at to decide? Let's take a closer look at the extra features provided by dataclasses...
The existing definition of SimpleNameSpace is as follows:
A simple object subclass that provides attribute access to its namespace, as well as a meaningful repr.
The python docs then go on to say that it provides a simple __init__
, __repr__
and __eq__
implementation. Comparing this to PEP 557, dataclasses also give you options for:
__init__
__repr__
__eq__
Clearly, then, you should use dataclasses if you care about ordering or immutability (or need the niche hashing control).
None that I can see, though you could argue that the initial "why?" covers other use cases.
I would point out that it also depends on how structured your data is. If there is a clear type/object model, dataclasses are more explicit as to what should be in the object. Also, class features like
__slots__
makes more sense with dataclasses than SimpleNamespace– Edward Minnix
Jun 29 at 12:02
__slots__
@EdwardMinnix Can you write that as an answer? It is a usecase worth discussing.
– xssChauhan
Jun 29 at 13:46
Dataclasses is much more like namedtuple
and the popular attrs package than SimpleNamespace
(which isn't even mentioned in the PEP). They serve two different intended purposes.
namedtuple
SimpleNamespace
Dataclasses
__init__
__hash__
__eq__
__slots__
SimpleNamespace
__slots__
From the SimpleNamespace
documentation:
SimpleNamespace
SimpleNamespace may be useful as a replacement for class NS: pass
. However, for a structured record type use namedtuple()
instead.
class NS: pass
namedtuple()
Since @dataclass
is supposed to replace a lot of the use cases of namedtuple
, named records/structs should be done with @dataclass
, not SimpleNamespace
.
@dataclass
namedtuple
@dataclass
SimpleNamespace
You may also want to look at this PyCon talk by Raymond Hettinger, where he goes into the backstory of @dataclass
and it's use.
@dataclass
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
SimpleNamespace only adds the attributes and a
__repr__
. dataclass adds much more, e.g.__eq__
,__hash__
, ..– L3viathan
Jun 28 at 12:05