FW: __getattr__ and other customization methods
brian at sweetapp.com
Mon Jun 18 08:52:42 CEST 2001
> Brian Quinlan wrote:
>> Is there any reason why getattr, when called on an instance, should
>> call that instance's __getattr__ method for other customization
>> methods (methods starting with __)?
> Otherwise you have no way of saying "this method does not exist".
That is not quite true. If you know before hand that you need access to
these methods (e.g. during construction), you can add them directly to
your instance. However, I understand that this might not be desirable or
possible when you are proxying a remote object.
> Consider an object that wraps another object - ie, a proxy.
> Some objects you proxy may provide a __len__ method, while others may
> not. However, Python changes behaviour based on the *existence* of
> method. Generally you will want your proxy to behave exactly the same
> as the proxied object.
If you want this to work in all cases, you will have to do some weird
stuff anyway. A lot of proxy implementations provide a __repr__ method,
which means that they cannot proxy that method. There probably isn't a
convenient way of proxying an __init__ call, etc.
> Further, it is, IMO, much cleaner this way. Special method names are
> just attributes. The lack of surprises here makes for a cleaner
> I agree there are gotchyas, but I don't think it should be any
I think classifying special methods as "just attributes" is understating
their participation in Python's object module. I would weight the need
to proxy customization methods with the frequencies of gotchyas. And if
you really need to proxy customization methods, there could be another
method to serve your needs:
More information about the Python-list