擅长:python、mysql、java
<p>如果你把台阶拆了,就更容易看到发生了什么。而不是</p>
<pre><code>self.my_df.loc[2, "B"] = "x"
</code></pre>
<p>考虑</p>
<pre><code>temp = self.my_df # Clearly this should call the get method
temp.loc[2, "B"] = "x" # Changes the pandas object
</code></pre>
<p>这两个片段获得相同的结果。不会调用setter,因为您没有分配给<code>my_obj</code>对象的<code>my_df</code>属性。您正在检索<code>self.my_df</code>(这是一个数据帧)的内容,然后对其进行操作。你知道吗</p>
<p><code>my_obj</code>对象只保存对<code>DataFrame</code>的引用,因此除非将<code>my_df</code>指向其他对象,否则不会调用setter。在您的代码中,<code>my_obj</code>对象仍然指向相同的数据帧,但是您已经操纵了数据帧的内容。你知道吗</p>