Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.025 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'amtsgericht': 0.07; 'attribute': 0.07; 'hrb': 0.07; 'attributes': 0.09; 'guys.': 0.09; 'iterate': 0.09; 'attr': 0.16; 'instance:': 0.16; 'literal.': 0.16; 'rainer': 0.16; 'ralf': 0.16; 'subject:between': 0.16; 'wrote:': 0.18; 'gmbh': 0.22; 'header:User-Agent:1': 0.23; 'string,': 0.24; 'germany': 0.24; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'there': 0.35; 'accessing': 0.36; 'useful': 0.36; 'thank': 0.38; 'ahead': 0.38; 'somebody': 0.38; 'to:addr:python- list': 0.38; 'rather': 0.38; 'explain': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; "you're": 0.61; 'name': 0.63; 'received:194': 0.64; 'skip:+ 10': 0.65; 'tel.:': 0.65; 'between': 0.67; 'press': 0.70; 'special': 0.74; 'received:192.168.57': 0.84; 'subject:skip:o 10': 0.84 Date: Wed, 11 Dec 2013 11:11:44 +0100 From: Johannes Schneider User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131103 Icedove/17.0.10 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Differences between obj.attribute and getattr(obj, "attribute") References: <52A82101.7030307@galileo-press.de> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386756687 news.xs4all.nl 2833 [2001:888:2000:d::a6]:60535 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61543 thank you guys. On 11.12.2013 10:36, Chris Angelico wrote: > 2013/12/11 Johannes Schneider : >> 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 use getattr when the attribute name comes from a string, rather > than a literal. There's no advantage to it when you know ahead of time > what attribute you're looking for. It's useful when you iterate over > dir(), for instance: > > print("You can call...") > n=0 > for attr in dir(x): > if callable(getattr(x,attr)): > print("x.%s()"%attr) > n+=1 > print("...",n," options.") > > ChrisA -- Johannes Schneider Webentwicklung johannes.schneider@galileo-press.de Tel.: +49.228.42150.xxx Galileo Press GmbH Rheinwerkallee 4 - 53227 Bonn - Germany Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax) http://www.galileo-press.de/ Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker HRB 8363 Amtsgericht Bonn