1

I'm new to python and came into this piece of code. From my previous programming knowledge, I'm assuming this method should return something(results). But why do some statements like self.children[0].query(rect, results) don't assign the return value to any variable while recursively calling the method?

def query(self, rect, results=None):

    if results is None:
        rect = normalize_rect(rect)
        results = set()

    if len(self.children) > 0:
        if rect[0] <= self.center[0]:
            if rect[1] <= self.center[1]:
                self.children[0].query(rect, results)
            if rect[3] > self.center[1]:
                self.children[1].query(rect, results)
        if rect[2] > self.center[0]:
            if rect[1] <= self.center[1]:
                self.children[2].query(rect, results)
            if rect[3] > self.center[1]:
                self.children[3].query(rect, results)

    for node in self.nodes:
        if (node.rect[2] > rect[0] and node.rect[0] <= rect[2] and
                node.rect[3] > rect[1] and node.rect[1] <= rect[3]):
            results.add(node.item)

    return results

1 Answer 1

2

You're right that the query function does return results, but it also modifies results on this line:

results.add(node.item)

A parameter used in this was is sometimes described as an "output parameter".

query is not a pure function. In those places where query is called without using its return value, query is being called for its side effects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.