4 ms·
What do you mean by this? Do namedtuples not give you this? >>> class MyClass(namedtuple('_MyClass', ['x', 'y', 'z'])): ... def foo(self): ... retu
by ericfrederich 9y ago
What do you mean by this?
Do namedtuples not give you this?
>>> class MyClass(namedtuple('_MyClass', ['x', 'y', 'z'])):
... def foo(self):
... return self.x * self.y + self.z
...
>>> x = MyClass(4, 5, 6)
>>> x.foo()
26
... or an attrs decorated class with frozen=True?
>>> import attr
>>> @attr.s(frozen=True)
... class MyClass:
... x = attr.ib()
... y = attr.ib()
... z = attr.ib()
... def foo(self):
... return self.x * self.y + self.z
...
>>> x = MyClass(4, 5, 6)
>>> x.foo()
26
- Dowwie 9y agoClose! ericvsmith, gvr, et al were inspired by attrs and have since been implementing a stdlib version of it, calling it "dataclass" see: https://github.com/ericvsmith/dataclasses https://github.com/ericvsmith/dataclasses and this comment thread is revealing: https://github.com/ericvsmith/dataclasses/issues/19 https://github.com/ericvsmith/dataclasses/issues/19
- ericfrederich 9y agoThanks for that link. Very informative interesting discussion. There must be a lot of desire for attrs from the stdlib developers.