Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61537
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.034 |
| X-Spam-Evidence | '*H*': 0.93; '*S*': 0.00; 'attribute': 0.07; 'attributes': 0.09; 'iterate': 0.09; 'cc:addr:python-list': 0.11; 'attr': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'instance:': 0.16; 'literal.': 0.16; 'subject:between': 0.16; 'cc:addr:python.org': 0.22; 'string,': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; 'received:google.com': 0.35; 'there': 0.35; 'accessing': 0.36; 'useful': 0.36; 'ahead': 0.38; 'somebody': 0.38; 'rather': 0.38; 'explain': 0.39; 'skip:p 20': 0.39; "you're": 0.61; 'name': 0.63; 'between': 0.67; 'special': 0.74; 'subject:skip:o 10': 0.84; 'to:none': 0.92 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=64heWj4WsMbahgxGzs4CRcxlhcCSdIIDcW3Xrx7/F/Q=; b=BZrB47+NiD1Tppc/mDcaf722WZLCPE/+mrRSZ1fBNIHc28Wz0OloluWtwvIJ/H49n7 Jpb3bXCfvleCOrhmXusBMhwqYfU6Yv7OW69zH9HwO52pMGgOIVivRBACmjMaugCpUh0I AGzLcSQcMygWWGNFyGfG4jxfiStWWQGx0phTX0eaJdslytzPd75T53nhZbwKhjKQ0aKl HtL+iEdG3T6gGpUcQ+uwl0Zxt4EHAowIcGF6SvGfQpbo0yORrAg9ku/9zqrffN3mhgI1 uWiKcoTegypDD2AzSEAo+TUHdDPsBCw23N5eiQ0MKsIFrJBwGD42KY4S74evkL5p8x+n 2wPA== |
| MIME-Version | 1.0 |
| X-Received | by 10.66.164.136 with SMTP id yq8mr455431pab.67.1386754582099; Wed, 11 Dec 2013 01:36:22 -0800 (PST) |
| In-Reply-To | <52A82101.7030307@galileo-press.de> |
| References | <52A82101.7030307@galileo-press.de> |
| Date | Wed, 11 Dec 2013 20:36:22 +1100 |
| Subject | Re: Differences between obj.attribute and getattr(obj, "attribute") |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| 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.3875.1386754591.18130.python-list@python.org> (permalink) |
| Lines | 20 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1386754591 news.xs4all.nl 2964 [2001:888:2000:d::a6]:44223 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:61537 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Differences between obj.attribute and getattr(obj, "attribute") Chris Angelico <rosuav@gmail.com> - 2013-12-11 20:36 +1100
csiph-web