Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104651
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: hasattr() or "x in y"? |
| Date | 2016-03-12 09:04 +1100 |
| Message-ID | <mailman.11.1457733894.12893.python-list@python.org> (permalink) |
| References | <nbve7c$nbo$1@dont-email.me> <nbvep7$pb3$1@dont-email.me> |
On Sat, Mar 12, 2016 at 8:53 AM, Charles T. Smith <cts.private.yahoo@gmail.com> wrote: > On Fri, 11 Mar 2016 21:44:27 +0000, Charles T. Smith wrote: > >> From the performance point of view, which is better: - hasattr() >> - x in y >> >> TIA >> cts > > > I just realized that "in" won't look back through the class hierarchy... > that clearly makes them not interchangable, but given we're only > interested in the current dict... They're still completely different - hasattr looks at attributes, but the 'in' operator looks at the object's members (in the case of a dictionary, the keys). There is a broad similarity between "hasattr(obj, attrname)" and "attrname in obj.__dict__", but if you're doing the latter, you have other problems than performance; attributes should be looked up as attributes, not as dictionary members. ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
hasattr() or "x in y"? "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-11 21:44 +0000
Re: hasattr() or "x in y"? Chris Angelico <rosuav@gmail.com> - 2016-03-12 08:55 +1100
Re: hasattr() or "x in y"? "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-11 21:53 +0000
Re: hasattr() or "x in y"? Chris Angelico <rosuav@gmail.com> - 2016-03-12 09:04 +1100
Re: hasattr() or "x in y"? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-11 22:05 +0000
Re: hasattr() or "x in y"? Grant Edwards <invalid@invalid.invalid> - 2016-03-11 22:00 +0000
Re: hasattr() or "x in y"? "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-11 22:18 +0000
Re: hasattr() or "x in y"? Grant Edwards <invalid@invalid.invalid> - 2016-03-11 22:30 +0000
csiph-web