dataclass中Java中的python等效

发布于 2025-01-26 16:50:12 字数 234 浏览 3 评论 0原文

是否可以

class MyAbstract {
   final int myFieldSomebodyHasToDefine;
}


class MyAbstractImplementation extends MyAbstract {
   final int myFieldSomebodyHasToDefine = 5;
}

在Python中使用Dataclasses之类的东西?

Is it possible to have something like

class MyAbstract {
   final int myFieldSomebodyHasToDefine;
}


class MyAbstractImplementation extends MyAbstract {
   final int myFieldSomebodyHasToDefine = 5;
}

using dataclasses in python?

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

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

发布评论

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

评论(1

夏天碎花小短裙 2025-02-02 16:50:12

如果您在版本3.8之前与Python解释器合作,则没有直接的方式。但是,自Python 3.8以来,最终的装饰器已添加到该语言中。从Python的打字模块导入后,您可以将其用于方法和类。
您也可以将最终类型用于值。

这是一个示例

from typing import final, Final


@final
class Base:
    @final
    def h(self)->None:
        print("old")


class Child(Base):
    # Bad overriding
    def h(self) -> None:
        print("new")


if __name__ == "__main__":
    b = Base()
    b.h()
    c = Child()
    c.h()
    RATE: Final = 3000
    # Bad value assignment
    RATE = 7
    print(RATE)

重要说明 python不会迫使开发人员最终和 final 。您仍然可以根据自己的意愿改变价值。对于开发人员而言,大多数信息的装饰者。

有关更多信息,您可以访问: https://peps.pypython.org/pep-0591/ < /a>

更新:这也是 dataclass 的实例,

@dataclass
class Item:
    """Class for keeping track of an item in inventory."""
    price: float
    quantity_on_hand: int = 0
    name:Final[str] = "ItemX"

    def total_cost(self) -> float:
        return self.unit_price * self.quantity_on_hand

如您所见, name 是最终字段。但是,您必须将默认值的最终值低于所有字段,而无需初始值。

If you are working with a python interpreter before version 3.8, there is no straightforward way. However, since python 3.8, the final decorator has been added to the language. After importing it from the typing module in python, you can use it for methods and classes.
You may also use FINAL type for values.

Here is an example

from typing import final, Final


@final
class Base:
    @final
    def h(self)->None:
        print("old")


class Child(Base):
    # Bad overriding
    def h(self) -> None:
        print("new")


if __name__ == "__main__":
    b = Base()
    b.h()
    c = Child()
    c.h()
    RATE: Final = 3000
    # Bad value assignment
    RATE = 7
    print(RATE)

Important note: Python does not force the developer with final and FINAL. You can yet change the values upon your wish. The decorators of mostly informative for developers.

For more information, you may visit: https://peps.python.org/pep-0591/

Update: This is also an instance for dataclass

@dataclass
class Item:
    """Class for keeping track of an item in inventory."""
    price: float
    quantity_on_hand: int = 0
    name:Final[str] = "ItemX"

    def total_cost(self) -> float:
        return self.unit_price * self.quantity_on_hand

As you can see, name is a final field. However, you must put the final values with a default value below all of the fields without an initial value.

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