When is overriding __getattr__ is useful?
Albert Hopkins
marduk at letterboxes.org
Mon Jan 7 11:47:47 EST 2013
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.
More information about the Python-list
mailing list