Python3-修改只读属性-未引发AttributeError



我是一名Java开发人员,正在学习Python。我使用的是Python 3.10.2。我试图理解Python中的只读变量(类似于Java中的final变量(,但我对Python处理事情的方式有点困惑。例如,下面是我的代码。

class Plant:
def __init__(self, name: str):
self.__name = name
@property
def name(self):
return self.__name

table_rose = Plant("Table Rose")
print(f"Name of the plant - {table_rose.name}")
#trying to change the name.. Expecting Attribute error in the below line.
table_rose.__name = "Croutons"
print(f"Name of the plant - {table_rose.name}")  #name didnt change..
print(f"Name of the plant - {table_rose.__name}") #It prinits Croutons.. Confusing how python handling things.

上面的代码打印所有三个打印函数,如下所示,但我预期的是AttributeError

我的代码输出

Name of the plant - Table Rose
Name of the plant - Table Rose
Name of the plant - Croutons

有人能解释一下我为什么没有出错吗?

一般来说,Python并不像Java那样做只读变量。因此,在查看Python代码时,当您可能期望Java类具有私有字段时,当所有内容都只是一个开放变量而没有封装在property装饰器中时,不要感到惊讶。

无论如何,在你的代码上:(

造成你困惑的原因是一种叫做名字篡改的东西。当你在一个类中创建一个以两个下划线开头的属性(例如self.__name(时,Python会在前面插入类名(使其实际上是self._Plant__name(,以避免子类化时发生名称冲突。

但是,当您在代码之外,并且您引用table_rose.__name时,Python不会自动进行名称篡改(因为您在类之外(,所以它只引用普通的.__name属性,完全缺少您想要的属性(称为._Plant__name(。

您可以使用dir()函数看到这一点。如果将对象传递给dir,它将返回该对象所有属性的列表。

>>> dir(table_rose)
['_Plant__name', ... , '__name', ...]

这意味着您正在创建一个值为'Croutons'的全新.__name变量,而不是更改现有变量。

最新更新