擅长:python、mysql、java
<blockquote>
<p>If I can access it directly after it has been declared by its name i.e <code>self.__test</code>, at which point does it become mangled?</p>
</blockquote>
<p>当您从类外部引用它时,它会损坏。你知道吗</p>
<p>您的第5行<code>print(self.__test)</code>没有损坏,因为您是从类中引用它的。只有当你在课外参考它时,它才会被弄坏。因此,你会打电话给</p>
<pre><code>print(x._Test__test)
</code></pre>
<p>注意,您还可以在类中使用<code>self._Test__test</code>。你知道吗</p>
<pre><code>class Test():
def __init__(self):
self.__test = "cats"
def get(self):
return self._Test__test
x = Test()
print(x.get()) ## cats
</code></pre>
<p>我认为这篇文章解释得很好:<a href="https://dbader.org/blog/meaning-of-underscores-in-python" rel="nofollow noreferrer"><em>The Meaning of Underscores in Python</em></a>。你知道吗</p>