小编Kha*_*ser的帖子

python名称修改和全局变量

#global variable
_test__x=13

class test:
    def __init__(self,x):
        self.__x=x
    def rex(self):
        return __x

d = test(5)
print(d.rex())
Run Code Online (Sandbox Code Playgroud)

如果我运行此代码,它将返回 13 我知道解释器应用了名称修饰,因此变量__x变成了_test__x 但这与全局变量相同。

我尝试使用,self._test__x但没有用

如何访问__x声明的变量__init __.py而不是全局变量?

python python-3.x

5
推荐指数
1
解决办法
230
查看次数

标签 统计

python ×1

python-3.x ×1