nested exceptions?
Aahz
aahz at pythoncraft.com
Sat Apr 20 20:30:31 EDT 2002
In article <roy-AAA3A1.19193820042002 at news1.panix.com>,
Roy Smith <roy at panix.com> wrote:
>
>I want to look an oid up in my dictionary, and if it's not there, I want to
>keep working my way up the tree until I find it. Thus, if
>'1.3.6.1.4.3.5.66.3.4' isn't a key, I want to try '1.3.6.1.4.3.5.66.3', and
>then '1.3.6.1.4.3.5.66', and then '1.3.6.1.4.3.5', and so on until I find
>it (if nothing else, '1' is guaranteed to be a key).
>
>What I've come up with uses recursion:
>
> def getBaseObject (self, objects, oid):
> try:
> return objects[oid]
> except KeyError:
> return self.getBaseObject (objects, self.getParentOid (oid))
I'd write this as
while not objects.has_key(oid):
oid = self.getParentOid(oid)
return objects[oid]
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
What if there were no rhetorical questions?
More information about the Python-list
mailing list