0

The following code from Geeks for Geeks to calculate the maximum path sum in Binary Tree.

In the function findMaxSum() they declared a variable as findMaxUtil.res What does that means ?

I saw this question on SOF If function.variable inside a function it means that the function is kind of an object.But in this example the function name and variable are out of the original function. Would someone explain that please with a clear example!

class Node: 
    def __init__(self, data): 
        self.data = data 
        self.left = None
        self.right = None

def findMaxUtil(root): 
    if root is None: 
        return 0 

    l = findMaxUtil(root.left) 
    r = findMaxUtil(root.right) 

    max_single = max(max(l, r) + root.data, root.data) 
    max_top = max(max_single, l+r+ root.data) 
    findMaxUtil.res = max(findMaxUtil.res, max_top)  

    return max_single 

def findMaxSum(root): 

    findMaxUtil.res = float("-inf")  ## This line
    findMaxUtil(root) 
    return findMaxUtil.res           ## and this line
3
  • 1
    That's a terrible approach - they're basically just using a global variable. They could have easily written this without any global state. Commented Dec 31, 2018 at 18:21
  • 1
    OTOH, better to use a global variable that's attached to the function than a global variable that's polluting module scope. So it's a bad approach, but it's better than the more obvious way of doing that bad approach. Commented Dec 31, 2018 at 18:23
  • ...btw, I think if they'd called it global_max rather than res it might have been more clear to the reader. Commented Dec 31, 2018 at 18:25

1 Answer 1

5

Functions are objects. They can have attributes like any other objects; there's no special syntax-level meaning.

Presumably, the intent in this case is to have what some other languages would call a "static" variable -- one that exists global to the function itself, vs being scoped to an individual call.


Demonstrating that even a trivial noop function can have variables hung off it:

def example():
    pass

example.foo = "hello"

print(example.foo) # prints "hello"
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much. But I noticed the code will not work if I delete the function name before the variable res. In this case, how to go over this and make it work with normal global variable?
If at module scope you set res = 0, then putting global res inside the function before its first reference to res will ensure that it uses the global. But don't do that -- global scope is a limited resource, and should be conserved where reasonable. (This fits with the "namespaces are one honking great idea" part of the Zen of Python -- you're using the function object as a namespace for the associated global).
So the best way to do it is to keep it as they did right ? Thanks again
Well, no, the best way is not to use globals at all, f/e, by making the function into a true class and referring to self.result or such. Same effect, but less surprising to readers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.