nested exceptions?
Aahz
aahz at pythoncraft.com
Sun Apr 21 09:45:35 EDT 2002
In article <a9t177$pi5$1 at panix1.panix.com>, Aahz <aahz at pythoncraft.com> wrote:
>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]
...and if you're using Python 2.2, write it as
while oid not in objects:
Hmmmm.... it occurs to me that those of us teaching Python will need to
be *very* careful in making sure that people avoid 'in' over lists.
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
What if there were no rhetorical questions?
More information about the Python-list
mailing list