[Python-ideas] "on" statement
Nick Coghlan
ncoghlan at gmail.com
Thu Apr 1 17:47:58 CEST 2010
Robert Madole wrote:
> So I just wrote my 1-gazillionth "if hasattr('method', my_object):"
> and wanted badly to write this.
>
> if 'method' on my_object:
>
> I'm not the most experienced Python user, but as far as I can tell
> hasattr is the preferred way of checking if a method exists.
>
> Please correct me if there is a better way
Most Python code should just assume the method is present and let the
AttributeError fly if it isn't.
If you genuinely need to check for presence of a method, then either
catching the AttributeError or using hasattr is fine.
This is a relatively rare requirement though, so the chance of getting
dedicated syntax (and a new keyword, no less!) to support it is
basically nil.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
More information about the Python-ideas
mailing list