0

Can this dataclass declaration:

@dataclass
class Point:
  x: float
  y: float
  z: float

be rewritten in order to reduce boilerplate and resemble something like this:

@dataclass
class Point:
  x, y, z: float
0

1 Answer 1

1

This is not really possible. The @dataclass decorator uses annotations to find the fields, and you can't have one annotation for multiple variables, see How to declare multiple variables with type annotation syntax in Python?.

An alternative would be to use the make_dataclass function, but I suppose it will not work well with static type checkers and other tooling:

from dataclasses import make_dataclass

Point = make_dataclass(
    "Point",
    [(n, float) for n in "xyz"]
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.