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
ananya
  • 879
  • 1
  • 7
  • 14
  • 1
    That's a terrible approach - they're basically just using a global variable. They could have easily written this without any global state. – user2357112 Dec 31 '18 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. – Charles Duffy Dec 31 '18 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. – Charles Duffy Dec 31 '18 at 18:25

1 Answers1

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"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 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? – ananya Dec 31 '18 at 18:27
  • 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). – Charles Duffy Dec 31 '18 at 18:28
  • So the best way to do it is to keep it as they did right ? Thanks again – ananya Dec 31 '18 at 18:29
  • 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. – Charles Duffy Dec 31 '18 at 18:30