Feature or enhancement
Proposal:
In current Python, the expression:
only works if the type defines a mapping or sequence slot.
If it does not, Python immediately raises TypeError, even if the object does provide a __getitem__ method dynamically.
This leads to surprising limitations for objects that delegate or map behavior to another object.
Example
Suppose we have an object acting as a dynamic view or proxy:
class ConfigView:
def __init__(self, target):
self.target = target
def __getattr__(self, name):
# forward lookups to the underlying object
return getattr(self.target, name)
If the underlying object implements __getitem__, we would naturally expect:
to work the same as:
But currently it fails with:
TypeError: 'ConfigView' object is not subscriptable
even though __getitem__ is available via attribute lookup.
Proposal
When a type does not provide a mapping or sequence slot, allow obj[key] to fall back to an attribute lookup for __getitem__, following normal attribute resolution rules (__getattribute__, __getattr__, delegation, etc.).
This would make subscripting consistent with other special methods that already allow attribute-based fallback, and it would significantly improve the behavior of dynamic proxy objects.
I would like to hear thoughts on whether this fallback would be acceptable as an enhancement.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
Feature or enhancement
Proposal:
In current Python, the expression:
only works if the type defines a mapping or sequence slot.
If it does not, Python immediately raises
TypeError, even if the object does provide a__getitem__method dynamically.This leads to surprising limitations for objects that delegate or map behavior to another object.
Example
Suppose we have an object acting as a dynamic view or proxy:
If the underlying object implements
__getitem__, we would naturally expect:to work the same as:
But currently it fails with:
even though
__getitem__is available via attribute lookup.Proposal
When a type does not provide a mapping or sequence slot, allow
obj[key]to fall back to an attribute lookup for__getitem__, following normal attribute resolution rules (__getattribute__,__getattr__, delegation, etc.).This would make subscripting consistent with other special methods that already allow attribute-based fallback, and it would significantly improve the behavior of dynamic proxy objects.
I would like to hear thoughts on whether this fallback would be acceptable as an enhancement.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
obj[key]when no mapping or sequence slot is defined #141981