morfizm (morfizm) wrote,
morfizm
morfizm

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: http://tohtml.com/python/)
 
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: http://morfizm.livejournal.com/472705.html
Tags: fun, in english, polls questions and social games, software development, work
Subscribe
  • Post a new comment

    Error

    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.
  • 40 comments