Question on overriding implicit lookups

David Ells ells.david at gmail.com
Tue Oct 2 11:16:41 EDT 2007


In Python we have a wonderful facility for customizing attribute
access by defining __getattr__ or __getattribute__ in our classes.
Unfortunately (or perhaps fortunately, for reasons I don't know), this
facility only works for explicit attribute access, i.e. accessing
foo.bar. What I am in need of, for reasons I'll keep out of the
discussion for now, is a similar facility for implicit attribute
access, such as the call to foo.__len__ when something like len(foo)
is executed. I would like some way to customize access during these
implicit lookups without having to define each one in the class
myself. Essentially what I am looking for is a mechanism like
__getattribute__, only one that will work for these implicit lookups
to functions like __len__, __str__, __getitem__, and so on. Hopefully
my question makes sense.




More information about the Python-list mailing list