News for hackers. Stuff that matters.
Re:The Data Classes Decorator by Ciantic on Sunday December 12, 2021 @06:16AM (#29528704)
Interesting to find this here, if someone else haven't followed Python3, there is also NamedTuple which for all purposes is like immutable record:

    from typing import NamedTuple
    class Coord(NamedTuple):
        x: int
        y: int
I would prefer immutable records over dataclasses on many occasions.
Re:The Data Classes Decorator by 331c8c71 on Sunday December 12, 2021 @06:51AM (#29528838)
Long gone are the days of "There should be one-- and preferably only one --obvious way to do it" from PEP 20 ("Python Zen") and it's a pity IMO.
Re:The Data Classes Decorator by q3k on Sunday December 12, 2021 @06:57AM (#29528871)
Yeah, Python is basically slowly becoming a TypeScript.

On one one hand: yay types. On the other hand… yeah, this isn’t Python anymore. What used to be a fairly simple language with some well known quirks evolved into an ungrokable katamari of every single possible language feature, a duct taped mush of pieces from different puzzle sets.

I guess it’s a victim of its own success. I just wish it stayed good old, Python, and people would just use different languages for different needs instead of needing one general purpose language for everything.

Re:The Data Classes Decorator by Banana699 on Sunday December 12, 2021 @07:37AM (#29529082)
I don't see how this is duct-tabing, decorators are very straightforward syntax sugar over Python's everything-is-an-object mentality

@decorator some_func

is simply

some_func = decorator(some_func)

There is no C++style "Here is 100 more arcane rules and 1000 exceptions and meta-rules that govern their interaction and application". It's all consistent additions to the language that does nothing but abstract out what you could do by hand but more verbose-ly and error-prone-ly. In fact, nothing in the above comments except type annotations are "language features" at all, they are libraries, and not even implicitly imported at that.

Re:The Data Classes Decorator by formerly_proven on Sunday December 12, 2021 @08:04AM (#29529285)
And almost nothing of these are actually language features but in the library.
Re:The Data Classes Decorator by angelzen on Sunday December 12, 2021 @08:35AM (#29529480)
"Python is basically slowly becoming a TypeScript." If only.
Re:The Data Classes Decorator by JoBrad on Sunday December 12, 2021 @01:50PM (#29532352)
Part of the reason I wrote my last app in Node/Typescript is because Python’s typing was such crap and MyPy wasn’t mature enough at the time.
Re:The Data Classes Decorator by woadwarrior01 on Sunday December 12, 2021 @01:35PM (#29532217)
The `eval()` in the namedtuple factory function[1] makes it a bit kludgy, IMO.

[1]: https://github.com/python/cpython/blob/3.10/Lib/collections/...

Re:The Data Classes Decorator by BurningFrog on Sunday December 12, 2021 @07:25AM (#29529005)
You can pass frozen=True to the @dataclass decoratot.
Re:The Data Classes Decorator by pletnes on Sunday December 12, 2021 @10:06AM (#29530165)
Dataclasses can optionally be «frozen» / immutable IIRC.

Namedtuple is intended for when you need a tuple. I’ve used it for e.g numpy array shapes, so image data has names (imgshape.width instead of imgshape[1]). There, you need an actual tuple (or subclass of tuple if you want).

Re:The Data Classes Decorator by lcnmrn on Sunday December 12, 2021 @08:18AM (#29529382)
NamedTuples work with and without type hints, but Dataclasses only work with type hints: https://subreply.com/reply/lsk
Re:The Data Classes Decorator by ericvsmith on Sunday December 12, 2021 @12:54PM (#29531790)
dataclasses author here.

That's not true. You can use make_dataclass the same way you can use namedtuple:

  >>> from dataclasses import make_dataclass
  >>> A = make_dataclass("A", ["x", "y", "z"])
  >>> A(1, 2, 3)
  A(x=1, y=2, z=3)
It is true that if you want to use @dataclass you have to use annotations, just the way you do with typing.NamedTuple. But as others have noted, the annotation is mostly ignored.

[edited for formatting]

Re:The Data Classes Decorator by lcnmrn on Friday December 17, 2021 @03:19AM (#29589375)
Can you assign defaults with make_dataclass() but no types?
Re:The Data Classes Decorator by vsmhn on Sunday December 12, 2021 @06:49AM (#29528829)
NamedTuple works great for a lot of cases, but not always. For example, when dealing with attribute defaults for mutable collections. Dataclasses have a `default_factory` that is used in these occasions.
Re:The Data Classes Decorator by JoBrad on Sunday December 12, 2021 @01:51PM (#29532367)
If I recall correctly, NamedTuple doesn’t support calculated properties, but a Data class does (as the first example shows).