"has" Operator

Fredrik Lundh fredrik at pythonware.com
Sat Jul 7 18:15:50 EDT 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:
>     foo()
> else:
>     bar()

try this:

if hasattr(myobject, "someattribute"):
    spam()
else:
    egg()

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list