maxywb maxywb - 5 months ago 9
Python Question

Where am I in a chain of indexing?

If I have some code like this:

x.y.z = 12


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

foo = x.y.z # situation 1
bar = x.y.z.bar # situation 2


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

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

I worry that the answer to this question is "you can't do that" since it is impossible to override
=
like you can in C++.

Answer

There is no way to do this with python overrides. The only way is to have a known member that means "the end." For example, if you wanted to know which member was being set in a long chain of indexes you'd need some kind of setter:

x.y.z.set(some_value)
Comments