Which idiom is better? try: ... except Attr..Err or hasattr?

Brad Howes bradh at mediaone.net
Thu Jan 27 23:20:36 EST 2000


"P.J.W.S. Vrijlandt" <P.J.W.S.VRIJLANDT at INT.azg.nl> writes:

> According to the manual,  hasattr is implemented by calling getattr 
> and seeing if an exception occurs, so hasattr will not be faster than 
> try...except...

A quick look at the source reveals that the builtin hasattr indeed uses the
object method getattr. However it clears any exception that getattr may post
for the Python interpreter loop to handle before returning, so there's no
exception processing being done. I bet hasattr is faster.

Brad

-- 
    "Were people this stupid before TV?" -- _White Noise_ by Don DeLillo



More information about the Python-list mailing list