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


Groups > comp.lang.python > #61538

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

References <52A82101.7030307@galileo-press.de> <l89bc2$cb1$1@ger.gmane.org>
Date 2013-12-11 20:38 +1100
Subject Re: Differences between obj.attribute and getattr(obj, "attribute")
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3876.1386754732.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Dec 11, 2013 at 8:30 PM, Jurko Gospodnetić
<jurko.gospodnetic@pke.hr> wrote:
>   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.

It's pretty easy to make sure no other code caused the exception -
just have no other code inside the try block.

x.foo(input().split(" ")[4])  # Might throw from any point in the expression

try:
   func = x.foo
except AttributeError:
   deal_with_error
func(......)

ChrisA

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


Thread

Re: Differences between obj.attribute and getattr(obj, "attribute") Chris Angelico <rosuav@gmail.com> - 2013-12-11 20:38 +1100

csiph-web