Which idiom is better? try: ... except Attr..Err or hasattr?
Darrell
darrell at dorb.com
Thu Jan 27 05:23:39 EST 2000
From: "P.J.W.S. Vrijlandt"
>
> 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...
>
Looks like hasattr is still faster.
from time import *
def f1():
t1=time()
for x in range(1000000):
if hasattr(dic, 'xx'):
pass
print time()-t1
def f2():
t1=time()
for x in range(1000000):
try:
dic['xxx']
except:
pass
print time()-t1
f1()
f2()
6.84399998188
40.1719999313
--Darrell
More information about the Python-list
mailing list