具有类型提示的 const 方法的 python 等效项
我正在尝试找到与 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它不必是类型提示解决方案,那么您可以执行 PIG208 在评论中建议的操作。即,您可以创建一个装饰器来覆盖 __setattr__ 方法,以便在修改方法内的任何属性时引发异常。
这也适合在每个方法的基础上附加:
然后您可以像任何其他装饰器一样使用它:
不幸的是,缺点是该方法不会像类型提示那样在 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:
You can then use it the same as any other 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.不存在这样的事情。
在 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.