【发布时间】:2021-08-05 00:16:50
【问题描述】:
假设我有一个 A 类
class A:
__slots__ = ['_x']
def __init__(self):
self._x = 10
@property
def x(self):
return self._x
并且我想限制从除其他实例的方法之外的任何地方分配给a._x。
我该怎么做?这在 Python 中是否可行?
问题是要编写一个类,其属性只能在特定方法内更改,而不能直接从外部更改。
【问题讨论】:
标签: python python-3.x oop access-modifiers