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


Groups > comp.lang.python > #61536

Re: Differences between obj.attribute and getattr(obj, "attribute")

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'attribute': 0.07; 'exists.': 0.07; 'suppose': 0.07; 'attributes': 0.09; 'exception.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'assume': 0.14; 'helps.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:between': 0.16; 'exception': 0.16; 'student': 0.16; 'wrote:': 0.18; 'module': 0.19; 'example': 0.22; 'header:User-Agent:1': 0.23; 'module,': 0.24; 'string,': 0.24; 'file.': 0.24; 'references': 0.26; 'suggested': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'points': 0.29; 'code': 0.31; 'cases': 0.33; 'problem': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'accessing': 0.36; 'method': 0.36; 'list': 0.37; 'e.g.': 0.38; 'somebody': 0.38; 'to:addr:python-list': 0.38; 'explain': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'most': 0.60; 'hope': 0.61; 'free': 0.61; 'new': 0.61; 'from:charset:utf-8': 0.61; 'simply': 0.61; 'between': 0.67; 'caused': 0.69; 'special': 0.74; 'real- life': 0.84; 'subject:skip:o 10': 0.84; 'received:31': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Jurko Gospodnetić <jurko.gospodnetic@pke.hr>
Subject Re: Differences between obj.attribute and getattr(obj, "attribute")
Date Wed, 11 Dec 2013 10:30:53 +0100
Organization PKE sistemi d.o.o.
References <52A82101.7030307@galileo-press.de>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 31.217.0.48
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.1.1
In-Reply-To <52A82101.7030307@galileo-press.de>
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3874.1386754268.18130.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1386754268 news.xs4all.nl 2888 [2001:888:2000:d::a6]:41659
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:61536

Show key headers only | View raw


   Hi.

On 11.12.2013. 9:23, Johannes Schneider wrote:
> can somebody explain me the difference between accessing attributes via
> obj.attribute and getattr(obj, "attribute")?
>
> Is there a special reason or advantage when using getattr?

   You can not use obj.attribute if you have the word 'attribute' as a 
string, e.g. if you read it from a configuration file.

   Also, you can not test whether an object has an attribute when using 
the object.attribute access method without raising/catching an exception 
and then it can be hard to make sure no other code caused the exception.

   But I suppose in most real-life cases both of the suggested access 
methods are equivalent, since most of the time you really do not have 
names of your attributes as strings and are free to simply assume the 
attribute exists.

   One example problem I had recently where I think you have to use 
getattr(): list the names of all global references in the __builtins__ 
module and, a the type of object each of those references points to.

   [Ran into this one by accident as a part of demonstrating to a new 
Python student what names Python recognizes in some module, and what 
names require additional imports.]

   Hope this helps.

   Best regards,
     Jurko Gospodnetić

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


Thread

Re: Differences between obj.attribute and getattr(obj, "attribute") Jurko Gospodnetić <jurko.gospodnetic@pke.hr> - 2013-12-11 10:30 +0100

csiph-web