morfizm (morfizm) wrote,

Tricky Python

Find a bad bug in this code (exercise; comments screened).

# collects tags from root node and all nodes reachable via item[attribute]=value edges,
# including all "None" edges
def evaluate_tree(self, item):
    result = self.tags
    if not self.leaf_node():
        if item.has_key(self.attribute):
            # item[] values can't be None
            for value in item[self.attribute]:
                if self.value_map.has_key(value):
                    result = result | self.value_map[value].evaluate_tree(item)
        if self.value_map.has_key(None):
            result = result | self.value_map[None].evaluate_tree(item)
    return result
(Syntax highligher used:
Upd.: I've updated, adding a couple of comments. The bug can be found without making tricky assumptions. Everything what's necessary is already in the code.

Upd2.: Comments are unscreened, the bug is explained here:
Tags: fun, in english, polls questions and social games, software development, work

  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.