[openstack-dev] [Review] Use of exception for non-exceptional cases
Nachi Ueno
nachi at ntti3.com
Wed Jul 10 19:01:10 UTC 2013
Hi Mark
Thank you for your answering
>
> I don't think this statement contradicts the intent of EAFP.
I got it :)
>> Personally, I prefer not to use exception for such cases.
>
> My instinct is the same, but EAFP does seem to be the python way. There
> are times I can tolerate the EAFP approach but, even then, I generally
> think LBYL is cleaner.
ok, so i'm worrying about the case one reviewer says to use LBYL, and
the other mentioning EAFP.
so it is great if we could some some criteria for this.
> I can live with something like this:
>
> try:
> return obj.foo
> except AttributeError:
> pass
>
> but this is obviously broken:
>
> try:
> return self.do_something(obj.foo)
> except AttributeError:
> pass
>
> since AttributeError will mask a typo with the do_something() call or an
> AttributeError raised from inside do_something()
>
> But I fail to see what's wrong with this:
>
> if hasattr(obj, 'foo'):
> return obj.foo
>
> Cheers,
> Mark.
>
>
> _______________________________________________
> OpenStack-dev mailing list
> OpenStack-dev at lists.openstack.org
> http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
More information about the OpenStack-dev
mailing list