struct Vec2 {
var x: Float
var y: Float
}
var pos = Vec2(x: 42, y: -42)
Free memberwise initializer for structs. You can also provide default values by appending = val to the members.
The @dataclass proposal looks closer still.
You don’t get free comparison operators (but it’s getting there) so this is nicer in that regard.
attrs has been successful enough that
PSFCPython core is investigating adding a stripped-down version to the 3.7 standard library.It’s not the PSF, it’s the CPython core developers. The PSF does not direct the development of the Python language.
This looks really nice. Simple, does what it does, gets out of the way.
Sounded unnecessary, but http://www.attrs.org/en/stable/why.html explains the rationale well, in contrast with named tuples and other things.
This gets close to a thing I enjoy in Swift:
Free memberwise initializer for structs. You can also provide default values by appending
= valto the members. The@dataclassproposal looks closer still.You don’t get free comparison operators (but it’s getting there) so this is nicer in that regard.
Yup I think this is basically “value types” for Python! I wanted this a long time ago!
Namedtuple was close but the syntax is a bit awkward.
In C99 you can do this:
It’s funny that statically typed languages like Swift and Go cover this better than a dynamically typed language like Python ;-)
Not sure what this has to do with dynamic vs static typing.