News for hackers. Stuff that matters.
Re:Proposing a Struct Syntax for Python by tln on Wednesday June 14, 2023 @01:18PM (#36329208)
This is already handled with dataclasses

https://docs.python.org/3/library/dataclasses.html#module-da...

..and the syntax is way better, handling more members, with room to document, handling methods, and not conflicting with Mojo or stdlib

Re:Proposing a Struct Syntax for Python by LoganDark on Wednesday June 14, 2023 @01:22PM (#36329261)
Doesn't support algebraic data types, though.

FWIW I think this article mentioning them at all is sort of weird because the article doesn't propose them either, lmao

Re:Proposing a Struct Syntax for Python by tikhonj on Wednesday June 14, 2023 @01:32PM (#36329411)
Right, the article talks about "type Point = Point1D | Point2D | Point3D" syntax at the end... which would work just as well with dataclasses as with structs!
Re:Proposing a Struct Syntax for Python by masklinn on Wednesday June 14, 2023 @03:53PM (#36331637)
It already does too. Just remove the `type` bit and you have an alias for a union, which has been supported since typing was added (though the `|` syntax was only added in 3.10). And AFAIK the type checkers do handle completeness on unions.
Re:Proposing a Struct Syntax for Python by fritzo on Wednesday June 14, 2023 @02:12PM (#36330091)
Do you mean Python's lack of a labeled union type?
Re:Proposing a Struct Syntax for Python by Spivak on Wednesday June 14, 2023 @02:30PM (#36330416)
Pydantic does though.

    class A(BaseModel):
      status: Literal["success"]

    class B(BaseModel):
      status: Literal["error"]

    class C(BaseModel, ABC):
      __root__: Union[A, B] = Field(discriminator="status")

    aorb = C.parse_obj(blah…).__root__

    if aorb.status == "error":
      # aorb will type check as B.
Re:Proposing a Struct Syntax for Python by b33j0r on Wednesday June 14, 2023 @03:16PM (#36331097)
You are a friend and a scholar.

I was looking for a solution in pydantic that is wildly relevant. Thanks, I didn’t realize that was implemented/existed.

My only objection is using ABC in your MRO. Haha, just a little bit overloaded with abstract base classes and using A, B, and C.

I got it eventually! Cool.

Re:Proposing a Struct Syntax for Python by Spivak on Wednesday June 14, 2023 @04:28PM (#36332130)
Ya know what, on reading that that does seem confusing. My brain had long since internalized ABC as a single token I didn't even notice.
Re:Proposing a Struct Syntax for Python by LoganDark on Wednesday June 14, 2023 @04:32PM (#36332177)
I believe this is a good example of type narrowing :)
Re:Proposing a Struct Syntax for Python by LoganDark on Wednesday June 14, 2023 @04:31PM (#36332170)
Yeah. Been spoiled by Rust.
Re:Proposing a Struct Syntax for Python by contravariant on Wednesday June 14, 2023 @04:49PM (#36332435)
It doesn't seem that far off though, it does have a union type and with pattern matching you can achieve pretty much the same thing (sure you may need a few extra classes if the types aren't distinct already but that seems like a small difference).

I don't know if type checkers can figure out if a pattern match statement is exhaustive yet though, that would be interesting.

Re:Proposing a Struct Syntax for Python by joiguru on Wednesday June 14, 2023 @02:29PM (#36330392)
Which Python 3.10 and static type checkers much of the benefits of algebraic data types is already available [1].

[1]: https://stackoverflow.com/questions/16258553/how-can-i-defin...

Re:Proposing a Struct Syntax for Python by masklinn on Wednesday June 14, 2023 @03:50PM (#36331589)
The typed equivalent of a sum type in python is a union (https://docs.python.org/3/library/typing.html#typing.Union). Works with pretty much every type you can think of, but has the usual union limitation that you can't have two variants of the same type (e.g. int | int | float).

Then again, you can just create newtype wrappers, and that's essentially the same thing as different constructors for a sum type.

Re:Proposing a Struct Syntax for Python by LoganDark on Wednesday June 14, 2023 @04:32PM (#36332191)
You'd need to create newtype wrappers anyway in order to tell them apart, I'd assume. Otherwise it wouldn't really be an algebraic data type as much as a... union type.
Re:Proposing a Struct Syntax for Python by masklinn on Wednesday June 14, 2023 @04:36PM (#36332235)
Hence “equivalent”. Of your variants all have trivial and non-overlapping payloads you can just use them as-is, or erlang-style tagged tuples, in most cases it’ll do fine.
Re:Proposing a Struct Syntax for Python by taeric on Wednesday June 14, 2023 @01:58PM (#36329864)
I was surprised at how dataclasses are not the first thing people are taught when it comes to "classes" in python. My two personal "you should probably learn how these work" things for beginners are pprint and dataclass. Goes a long long way to helping look at things.
Re:Proposing a Struct Syntax for Python by jasonpeacock on Wednesday June 14, 2023 @02:18PM (#36330185)
It's because the traditional teaching focus is OOP and not data-driven design.
Re:Proposing a Struct Syntax for Python by taeric on Wednesday June 14, 2023 @03:21PM (#36331166)
The part that blows my mind, on this, is that I see it in the "data science" community. An area that seems custom made for leaning on smaller or non-existent inheritance trees.
Re:Proposing a Struct Syntax for Python by xapata on Wednesday June 14, 2023 @03:04PM (#36330928)
Dataclasses wind up cumbersome over time. They're nice when the defaults are what you want. After a handful of default overrides, I'd rather have not used dataclasses.
Re:Proposing a Struct Syntax for Python by taeric on Wednesday June 14, 2023 @03:20PM (#36331151)
In the office job, I'd push on why the defaults didn't work out. In particular, I would be worried that I was specifically aiming to make future life a bit harder by not fitting with the defaults of dataclass.

To that end, I'm curious what defaults you didn't use?

Re:Proposing a Struct Syntax for Python by olejorgenb on Wednesday June 14, 2023 @02:49PM (#36330722)
This is the reasoning from the article for why its suggestion is better then dataclasses:

> - Typing is optional (there are still folks who don't want to lean into that and it simply isn't always necessary)

> - Class construction should be faster (which intuitively you would think shouldn't matter, but I have talked to folks where this is an actual concern, especially when startup time is critical)

> - Syntax typically leads to better tooling support since there's no ambiguity

> - Easier to teach than (data)classes, so can act as a stepping stone towards classes

> - Better semantics than dataclasses have by default (at least in my opinion )

Re:Proposing a Struct Syntax for Python by pwdisswordfishc on Wednesday June 14, 2023 @03:42PM (#36331475)
> - Typing is optional (there are still folks who don't want to lean into that and it simply isn't always necessary)

Someone who really wants to avoid type annotations at all costs can just use ": object" everywhere

Re:Proposing a Struct Syntax for Python by nomel on Wednesday June 14, 2023 @04:31PM (#36332167)
I believe it should be ": Any".
Re:Proposing a Struct Syntax for Python by agons on Wednesday June 14, 2023 @07:16PM (#36334203)
Hey, you're leaning into it too much!

Who are people who are against typing in this day and age?

Re:Proposing a Struct Syntax for Python by glaucon on Wednesday June 14, 2023 @09:21PM (#36335231)
> Who are people who are against typing in this day and age?

People who don't find the benefits of typing are worth the overhead?

Python is, after all, a dynamically-typed language, so it is, surely, not too surprising that those who use it might include those who want a ... dynamic language ?

If it's too much to take the joys of Java are freely available.

Re:Proposing a Struct Syntax for Python by nomel on Thursday June 15, 2023 @12:16AM (#36336322)
> are worth the overhead?

What overhead, specifically?

I've found it reduces overhead. Rather than using English to describe the types, in the comments/docstrings, I can just type the terse type hint in the function definition, and leave the rest to the documentation renderer. If you don't document anyways, sure.

Re:Proposing a Struct Syntax for Python by yuppiepuppie on Thursday June 15, 2023 @03:14AM (#36337374)
I use python daily and dont use typing. The reason being that I choose python for it readability. And I find typed python to be less readable than non-typed python. If I wanted a typed language, I wouldnt want python.
Re:Proposing a Struct Syntax for Python by IshKebab on Wednesday June 14, 2023 @03:10PM (#36331006)
> there are still folks who don't want to lean into that

Honestly why would you cater to people who want to write worse code? Should Python have a mode with implicit type coercion too because some people can't be bothered to type `int()`?

> Class construction should be faster.

It's Python. You accepted your code would be extremely slow going in. Fiddling with the syntax isn't going to make it fast.

Re:Proposing a Struct Syntax for Python by Groxx on Wednesday June 14, 2023 @03:44PM (#36331505)
Yeah, honestly I think if startup time is a real concern, python is simply the wrong choice. Slow startup is largely inherent in its design.

Not that it's not worth spending some effort to optimize, of course. But the lower bound is quite high even if you're very careful.