Where am I in a chain of indexing?
<p>If I have some code like this:</p>

<pre><code>x.y.z = 12
</code></pre>

<p>I can infer that the <code>z</code> member is being indexed from the call to <code>__setattr__</code>. However if I have something like this:</p>

<pre><code>foo = x.y.z # situation 1
bar = x.y.z.bar # situation 2
</code></pre>

<p>How can I determine which of the above situations I am in, if I care to do something special for <code>z</code> based on whether or not it is last in the chain of indexing? Is this kind of inference even possible in Python?</p>

<p>For more clarity let's assume I can change the implementation of all the objects being indexed, so using descriptors is wholly possible.</p>

<p>I worry that the answer to this question is "you can't do that" since <a href="https://stackoverflow.com/questions/11024646/is-it-possible-to-overload-python-assignment">it is impossible to override <code>=</code> like you can in C++.</a> </p>