kw_only和老虎机与旧版本的python兼容

发布于 2025-02-09 20:56:21 字数 743 浏览 2 评论 0原文

我如何使用新的kw_only插槽 python 3.10的功能/dataclasses.html#dataclasses.dataclass“ rel =“ nofollow noreferrer”> dataclass 同时还支持旧版本的Python?

我要设置kw_only的主要原因是,我可以拥有更多的置信度值转到正确的字段,而slots是针对我可能创建大量的对象并且不需要不必要的dict在幕后周围漂浮。

我最初认为使用类似的东西:

from dataclasses import dataclass

# check if we're using Python >= 3.10
if 'kw_only' in dataclass.__kwdefaults__:
  _dataclass = dataclass

  # redefine this to ignore new options
  def dataclass(cls, *, kw_only=False, slots=False, **kwargs):
    if cls is None:
      return _dataclass(*kwargs)
    return _dataclass(cls)

但是这导致Mypy抱怨重新定义的功能。

Python 3.8是我关心的最古老的版本。

How can I make use of the new kw_only and slots features available in Python 3.10's dataclass while also supporting older version of Python?

The main reason I want to set kw_only is so that I can have more confidence values go to the right field, and slots is for an object I'm likely creating lots of and don't want an unnecessary dict floating around behind the scenes.

I initially thought to use something like:

from dataclasses import dataclass

# check if we're using Python >= 3.10
if 'kw_only' in dataclass.__kwdefaults__:
  _dataclass = dataclass

  # redefine this to ignore new options
  def dataclass(cls, *, kw_only=False, slots=False, **kwargs):
    if cls is None:
      return _dataclass(*kwargs)
    return _dataclass(cls)

but this caused MyPy to complain about the function being redefined.

Python 3.8 is the oldest version I care about supporting personally.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早茶月光 2025-02-16 20:56:21

我已经通过使用以下方法解决了这一点:

from dataclasses import dataclass

# kw_only and slots were only introduced in Python 3.10
KW_ONLY_DATACLASS = dict(kw_only=True) if 'kw_only' in dataclass.__kwdefaults__ else {}
SLOTS_DATACLASS = dict(slots=True) if 'slots' in dataclass.__kwdefaults__ else {}

# set kw_only=True on Python 3.10, ignoring if not supported
@dataclass(**KW_ONLY_DATACLASS)
class Foo:
    foo: int

# and demo setting slots along with frozen
@dataclass(frozen=True, **SLOTS_DATACLASS)
class Bar:
    bar: int

这个想法是定义一组可以扩展的词典,并将填写适当的参数。

我正在寻找dataclass .__ kwdefaults __检查当前Python解释器支持哪些参数。似乎也可以使用dataclass .__代码__。co_varnames,但不是因为这需要更深入地看另一个级别。

I've solved this by using:

from dataclasses import dataclass

# kw_only and slots were only introduced in Python 3.10
KW_ONLY_DATACLASS = dict(kw_only=True) if 'kw_only' in dataclass.__kwdefaults__ else {}
SLOTS_DATACLASS = dict(slots=True) if 'slots' in dataclass.__kwdefaults__ else {}

# set kw_only=True on Python 3.10, ignoring if not supported
@dataclass(**KW_ONLY_DATACLASS)
class Foo:
    foo: int

# and demo setting slots along with frozen
@dataclass(frozen=True, **SLOTS_DATACLASS)
class Bar:
    bar: int

the idea is to define a set of dictionaries that can be expanded and will fill out the appropriate parameters.

I'm looking in dataclass.__kwdefaults__ to check which parameters are supported by the current Python interpreter. It also seems possible to use dataclass.__code__.co_varnames but didn't because this requires looking another level deeper.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文