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


Groups > comp.lang.python > #36364

Re: When is overriding __getattr__ is useful?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.109.133.86.MISMATCH!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <marduk@letterboxes.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'attribute': 0.05; 'override': 0.07; 'received:internal': 0.09; 'advance.': 0.15; 'cases': 0.15; 'message-id:@webmail.messagingengine.com': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:mail.srv.osa': 0.16; 'received:messagingengine.com': 0.16; 'received:nyi.mail.srv.osa': 0.16; 'received:osa': 0.16; 'received:srv.osa': 0.16; 'xmlrpclib': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'jan': 0.18; '>>>': 0.18; 'object.': 0.22; 'example': 0.23; 'allows': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'library.': 0.27; "doesn't": 0.28; 'exposed': 0.29; 'skip:_ 10': 0.29; 'to:addr:python-list': 0.33; 'subject:?': 0.35; 'there': 0.35; 'but': 0.36; 'method': 0.36; 'uses': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'received:10': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'service.': 0.61; 'header:Message- Id:1': 0.62; 'provide': 0.62; 'necessarily': 0.63; 'brown': 0.65; 'quality': 0.69; 'wire': 0.71; 'examples.': 0.84
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d=letterboxes.org; h=message-id:from:to:mime-version:content-transfer-encoding :content-type:in-reply-to:references:subject:date; s=mesmtp; bh= iahdaLuzbajCWQmj86aWZe4wRoE=; b=JRLPXLvaepE7QV3MYIvbOBN6m4O9THmQ nGEJTzPdCtblkXRBwPqfP3mPXudSgQMd0DbgmblqOLQwFNT4SLs1HI01CbMzbbSE Inn0EmKIuHskt+CyDUu9b5Yy2MY1jVd7Eam/3To1dSiXGCVu2GrUmPuccbN97YXF +3RltYB/jNY=
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=message-id:from:to:mime-version :content-transfer-encoding:content-type:in-reply-to:references :subject:date; s=smtpout; bh=iahdaLuzbajCWQmj86aWZe4wRoE=; b=a2T MLWR6jg7BSDYfk06uRKRYseebdM1Zy0LbVwPCFipdZKYZMxKZVNzHjsPAIqjHj7t F1XXgZcNWzo/svDCs6raTTF7/e8wHfKm6RbZ0hA6Jox5tz63dGBvNkP2ayyzg14h 2oGn0sBZq2euCDrQACmOl0K0WnCNCNcXdwfpBXaE=
X-Sasl-Enc LOlQOTBsAhbIUjbuG7OuPWaDzyRGNmlrRyZz6LYQHtGy 1357577267
From Albert Hopkins <marduk@letterboxes.org>
To python-list@python.org
MIME-Version 1.0
Content-Transfer-Encoding 7bit
Content-Type text/plain
X-Mailer MessagingEngine.com Webmail Interface - ajax-33d24f93
In-Reply-To <CABRP1o_d7WTrGtkY3LkQRxydTw0Nn4T14M2fMrrQ8dLqHd1ZGQ@mail.gmail.com>
References <CABRP1o_d7WTrGtkY3LkQRxydTw0Nn4T14M2fMrrQ8dLqHd1ZGQ@mail.gmail.com>
Subject Re: When is overriding __getattr__ is useful?
Date Mon, 07 Jan 2013 11:47:47 -0500
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.228.1357577269.2939.python-list@python.org> (permalink)
Lines 22
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1357577269 news.xs4all.nl 6911 [2001:888:2000:d::a6]:44346
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:36364

Show key headers only | 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