On Sat, Feb 22, 2014 at 11:14 PM, Stephen J. Turnbull <stephen@xemacs.org> wrote:
Antoine Pitrou writes:
On Sat, 22 Feb 2014 22:13:58 +1100 Chris Angelico <rosuav@gmail.com> wrote:
hasattr(x,"y") <-> (x.y or True except AttributeError: False)
But it's not the same. hasattr() returns a boolean, not an arbitrary value.
I think he meant
hasattr(x,"y") <-> (x.y and True except AttributeError: False)
No, I meant 'or' to ensure that an attribute holding a false value doesn't come up false. But if you really want a boolean, just wrap it up in bool(). My main point, though, was that most usage of hasattr is probing just before something gets used - something like this: if hasattr(obj, "attr"): blah blah obj.attr else: maybe use a default or maybe do nothing Some cases could become except-expressions; others could become try/except statements. In each case, the loss would be small. I'm not saying hasattr should be removed, just that it wouldn't have a strong case if it didn't already exist. ChrisA