slow try statements in python?
John Machin
sjmachin at lexicon.net
Wed Feb 19 16:09:12 EST 2003
Duncan Booth <duncan at NOSPAMrcp.co.uk> wrote in message news:<Xns932790E292E90duncanrcpcouk at 127.0.0.1>...
>
> In cases like this it can be worth getting a rough idea of the sort of
> times we are talking about. The code below tries accessing a key in a
> dictionary in 3 different ways.
> The second method takes 0.81 seconds whether or not the key is found.
> def testHasAttr(aDict):
> if hasattr(aDict, "xyz"):
> a = aDict["xyz"]
> else:
> a = "Not found"
> return a
>
I'm using Python 2.2 and it gives the following (expected as per the
manual) results:
>>> d = {"foo":1, "bar": 2}
>>> hasattr(d, "foo")
0
>>> [hasattr(d, x) for x in d.keys()]
[0, 0]
>>> [hasattr(d, x) for x in dir(d)]
[1, 1, 1, 1, etc etc]
>>>
What are you using? Parrot 0.000001a ???
More information about the Python-list
mailing list