For a class Foo
,
class Foo:
...
is there a way to call a specific method whenever Foo.XXX
(an arbitrary class attribute XXX
, such as bar
, bar2
, etc.) is being resolved, but NOT whenever Foo().XXX
(an arbitrary instance attribute) is resolved? To the best of my knowledge, overriding
def __getattr__(self, a):
...
applies to the second case only. I'd like to benefit from the fact that __getattr__
for instances is called only when the corresponding attribute is not found. Does anyone know how this should be solved? I've read through the manual and found nothing of relevance.
I don't particularly care what happens if someone tries to assign a value to any class or instance attribute of Foo
.
The use case (to avoid XY):
Class Foo
is a messenger. Instances of Foo
are basically tuples with extra steps. However, there are special cases of Foo
, which I'd like to store as class attributes of Foo
(I managed that using a special decorator). However, the number of special cases of Foo
is gradually growing while they differ very insignificantly. It would be very efficient code-wise for me to have this special function called whenever someone wants Foo.SPECIAL_CASE_123
, because mapping from "SPECIAL_CASE_123"
to the actual special case is very fast and very similar to mapping "SPECIAL_CASE_456"
to the other corresponding special case.