joonas at olen.to
Sat Jul 7 23:16:53 CEST 2001
Paul Sidorsky wrote:
> I was thinking that Python might benefit from a "has" operator that
> would allow you to type something like this:
> if myobject has someattribute:
> This would be roughly equivalent to:
> uselessvar = myobject.someattribute
> execpt AttributeError:
> bar() # myobject does not have someattribute
> foo() # myobject has someattribute
> ...but (IMO) the operator version would be much nicer and more
> readable. (This idea came about when I actually tried to type in the
> former! Python's syntax is so clean that it seemed to be a natural
> The Inform interactive-fiction language provides "has" and "hasnt"
> operators that do something similar so this is not without precendent.
> But for Python, it admittedly may be nothing more than syntactic sugar.
> What does everyone else think?
Use builtin function hasattr.
>>> import sys
More information about the Python-list