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

P.J.W.S. Vrijlandt P.J.W.S.VRIJLANDT at INT.azg.nl
Thu Jan 27 05:29:45 EST 2000


> From:          thantos at chancel.org (Alexander Williams)
> Subject:       Re: Which idiom is better? try: ... except Attr..Err or hasattr?
> Date:          Thu, 27 Jan 2000 09:27:03 GMT
> Organization:  Wyvern Enterprises/Palantir Heavy Industries
> Reply-to:      thantos at gw.total-web.net
>>  Which coding idiom is better?  The second form is one line 
>>  shorter
> 
> Personally, I prefer the former idiom with try/except; some object
> because its slightly slower than the other, but I find it clearer.
> 

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...

Patrick




More information about the Python-list mailing list