Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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)


This 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.


I've never seen this before. But yes, this does work. And that's very nice.

Thank you for the tip.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: