When is overriding __getattr__ is useful?

2013-01-07 Thread Rodrick Brown
Can someone provide an example why one would want to override __getattr__
and __getattribute__ in a class?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When is overriding __getattr__ is useful?

2013-01-07 Thread Albert Hopkins


On Mon, Jan 7, 2013, at 10:54 AM, Rodrick Brown wrote:
 Can someone provide an example why one would want to override __getattr__
 and __getattribute__ in a class?


They're good for cases when you want to provide an attribute-like
quality but you don't know the attribute in advance.

For example, the xmlrpclib uses __getattr__ to expose XML-RPC methods
over the wire when it doesn't necessarily know what methods are exposed
by the service.  This allows you do simply do

 service.method(*args)

And have the method seem like it's just a local method on an object.


There are countless other examples.  But that's just one that can be
found in the standard library.

-- 
http://mail.python.org/mailman/listinfo/python-list