4 ms·
You can also explicitly specify which arguments need to be keyword only using the KW_ONLY sentinel type annotation: from dataclasses import KW_ONLY @datac
by meander_water 1y ago
You can also explicitly specify which arguments need to be keyword only using the KW_ONLY sentinel type annotation:
from dataclasses import KW_ONLY
@dataclass
class Point:
x: float
_: KW_ONLY
y: float
z: float
p = Point(0, y=1.5, z=2.0)
- mixedmath 1y agoI've never seen this before. But yes, this does work. And that's very nice. Thank you for the tip.
- chipx86 1y agoThis is a great feature for those who want a mix of positional and keyword-only arguments. I should have mentioned originally (and I've since updated my post) that this and the kw_only= flag both require Python 3.10 and higher, so code that works with older versions can't opt into it yet.