maxywb maxywb - 1 year ago 64
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
member is being indexed from the call to
. However if I have something like this:

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

How can I determine which of the above situations I am in, if I care to do something special for
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 Source

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download