Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36364

Re: When is overriding __getattr__ is useful?

From Albert Hopkins <marduk@letterboxes.org>
References <CABRP1o_d7WTrGtkY3LkQRxydTw0Nn4T14M2fMrrQ8dLqHd1ZGQ@mail.gmail.com>
Subject Re: When is overriding __getattr__ is useful?
Date 2013-01-07 11:47 -0500
Newsgroups comp.lang.python
Message-ID <mailman.228.1357577269.2939.python-list@python.org> (permalink)

Show all headers | View raw



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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: When is overriding __getattr__ is useful? Albert Hopkins <marduk@letterboxes.org> - 2013-01-07 11:47 -0500

csiph-web