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


Groups > comp.lang.python > #61543

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

Date 2013-12-11 11:11 +0100
From Johannes Schneider <johannes.schneider@galileo-press.de>
Subject Re: Differences between obj.attribute and getattr(obj, "attribute")
References <52A82101.7030307@galileo-press.de> <CAPTjJmpnVHGgGiXZ2Cuj-K5k2JLo6qXysX2FEdM6MQ1tQDdS=A@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3879.1386756687.18130.python-list@python.org> (permalink)

Show all headers | View raw


thank you guys.
On 11.12.2013 10:36, Chris Angelico wrote:
> 2013/12/11 Johannes Schneider <johannes.schneider@galileo-press.de>:
>> 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

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


Thread

Re: Differences between obj.attribute and getattr(obj, "attribute") Johannes Schneider <johannes.schneider@galileo-press.de> - 2013-12-11 11:11 +0100

csiph-web