On Sat, Feb 22, 2014 at 8:20 PM, Antoine Pitrou <solipsis@pitrou.net> wrote:
On Sat, 22 Feb 2014 16:12:27 +0900 "Stephen J. Turnbull" <stephen@xemacs.org> wrote:
Note in support: I originally thought that "get" methods would be more efficient, but since Nick pointed out that "haveattr" is implemented by catching the exception (Yikes! LBYL implemented by using EAFP!), I assume that get methods also are (explicitly or implicitly) implemented that way.
Well, the only way to know that a key (or attribute) exists is to do the lookup. What else would you suggest?
And, yes, EAFP can avoid race conditions and the like (besides being more efficient with non-trivial keys).
Which means that, fundamentally, EAFP is the way to do it. So if PEP 463 expressions had existed from the beginning, hasattr() probably wouldn't have been written - people would just use an except-expression instead. ChrisA