具有类型提示的 const 方法的 python 等效项

发布于 2025-01-12 23:09:14 字数 865 浏览 2 评论 0原文

我正在尝试找到与 c++ const 方法等效的 python 方法。那是, 禁止更改其类的任何数据成员的方法。

from typing import Final

age: Final = 2
age += 3               # <----- warn about this -- works!

class Person:
    def __init__(self, age: int) -> None:
        self.age = age
    def happy_birthday(self: Final[Person]) -> None:
        self.age += 1  # <----- warn about this -- how?

我从未见过与 self 一起使用的类型提示,因此它看起来有点奇怪(并且不起作用)

main.py:4: error: Cannot assign to final name "age"
main.py: note: In member "happy_birthday" of class "Person":
main.py:9: error: Final can be only used as an outermost qualifier in a variable annotation  [misc]
        def happy_birthday(self: Final[Person]) -> None:
                                 ^

是否有任何其他方法可以实现此目的?请注意,这些属性仍应允许在其他方法中进行修改。

I'm trying to find the python equivalent of c++ const methods. That is,
methods that are forbidden to change any data member of their class.

from typing import Final

age: Final = 2
age += 3               # <----- warn about this -- works!

class Person:
    def __init__(self, age: int) -> None:
        self.age = age
    def happy_birthday(self: Final[Person]) -> None:
        self.age += 1  # <----- warn about this -- how?

I've never seen type hints used with self so it looks a bit weird (and doesn't work)

main.py:4: error: Cannot assign to final name "age"
main.py: note: In member "happy_birthday" of class "Person":
main.py:9: error: Final can be only used as an outermost qualifier in a variable annotation  [misc]
        def happy_birthday(self: Final[Person]) -> None:
                                 ^

Is there any other way to achieve this? Note that the attributes should still allow modification in other methods.

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

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

发布评论

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

评论(2

无人问我粥可暖 2025-01-19 23:09:14

如果它不必是类型提示解决方案,那么您可以执行 PIG208 在评论中建议的操作。即,您可以创建一个装饰器来覆盖 __setattr__ 方法,以便在修改方法内的任何属性时引发异常。

这也适合在每个方法的基础上附加:

def const_method(func):
    def wrapper(instance):
        class unsettable_class(instance.__class__):
            def __init__(self):
                super().__setattr__("__dict__", instance.__dict__)

            def __setattr__(self, attr, value):
                if hasattr(self, attr):
                    raise AttributeError(f"Trying to set value: {value} on the read-only attribute: {instance.__class__.__name__}.{attr}")
                super().__setattr__(attr, value)

        return func(unsettable_class())
    return wrapper

然后您可以像任何其他装饰器一样使用它:

class Person:
    def __init__(self, age: int) -> None:
        self.age = age

    @const_method
    def happy_birthday(self: "Person") -> None:
        self.age += 1 # throws an error since we are using the custom decorator

    def rebirth(self: "Person") -> None:
        self.age = 0 # will not throw an error since there is no decorator

不幸的是,缺点是该方法不会像类型提示那样在 IDE 中为您提供语法突出显示。

更新:您可以让自动完成功能以代码仍然是 mypy 正确的方式工作。使用 from __future__ import 注解 并删除 " ("Person" -> Person) 就可以了。

If it doesn't have to be a type-hint solution, then you can do what PIG208 suggested in the comments. i.e. you can create a decorator that overwrites the __setattr__ method to throw an exception when modifying any attributes inside the method.

This is also suitable to attach on a per-method basis:

def const_method(func):
    def wrapper(instance):
        class unsettable_class(instance.__class__):
            def __init__(self):
                super().__setattr__("__dict__", instance.__dict__)

            def __setattr__(self, attr, value):
                if hasattr(self, attr):
                    raise AttributeError(f"Trying to set value: {value} on the read-only attribute: {instance.__class__.__name__}.{attr}")
                super().__setattr__(attr, value)

        return func(unsettable_class())
    return wrapper

You can then use it the same as any other decorator:

class Person:
    def __init__(self, age: int) -> None:
        self.age = age

    @const_method
    def happy_birthday(self: "Person") -> None:
        self.age += 1 # throws an error since we are using the custom decorator

    def rebirth(self: "Person") -> None:
        self.age = 0 # will not throw an error since there is no decorator

Unfortunately, the downside is that this method won't give you syntax highlighting in your IDE like type hinting will though.

UPDATE: You can get autocompletion to work in a way that the code is still mypy-correct. Using from __future__ import annotations and dropping the " ("Person" -> Person) does the trick.

木有鱼丸 2025-01-19 23:09:14

不存在这样的事情。

在 C++ 中,存在 const 是出于历史和编译器优化原因,而与 Python 无关。这个概念似乎不太适合 Python 的面向对象编程模型,在该模型中,方法和数据结构形成了内聚单元:对象。

There is no such thing.

In C++, const is there for historical and compiler optimization reasons that are not relevant in Python. This notion does not seem to fit well in Python's object-oriented programming model, in which methods and data structures form cohesive units: the object.

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