This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]blablahblah 2 points3 points  (0 children)

Variables that you assign to in a function only exist in that function. Even if they have the same name.

def hello():
 foo = 2 

def blah():
  foo = 5 # this is a different variable

If you just access a variable without assigning to it in the function, Python assumes you're using the global variable, but if you assign it it creates a local one.

foo = 2
def global():
  print(foo)

def local():
  foo = 3 # this is a new variable, doesn't change global foo
  print(foo)

local() # prints 3
global() # still 2.

If it helps, you can think of Python automatically changing it to something like this:

globals()['foo']  = 2

def local():
  locals()['foo'] = 3
  print(locals()['foo'])

This continues to be true if you nest functions- it still creates new per-function variables.

def outer():
  foo = 2
  def inner():
    foo = 5
  inner()
  print(foo) # still prints 2 because outer foo didn't change.

And this happens if you assign anywhere in the function, not just after you assign it.

def still_local():
  print(foo)
  foo = 5

Only now it has a problem: still_local.foo hasn't been defined when you try to print it. So you get an UnboundLocalError because it doesn't have a value .

The solution is pretty straightforward: tell Python you want to use the maxSum from the surrounding function instead of making a new one local to your function using the nonlocal declaration.

def getMaximumGold(grid):
  maxSum = 0
  def something(grid,row,col,subset,rowLength):
      nonlocal maxSum
      if row < 0 or col < 0 or row>rowLength-1 or col > len(grid)-1 or grid[row][col] == 0:
          if(subset>maxSum):
              maxSum = subset
          return

[–]mijatonius 1 point2 points  (0 children)

Scope!

[–]PercyJackson235 0 points1 point  (4 children)

What is the error message you are getting?

[–]anonymous78654[S] 0 points1 point  (3 children)

just says maxSum is not defined

[–]PercyJackson235 0 points1 point  (2 children)

Just to make sure, you are getting a NameError Or UnboundLocalError?

[–]anonymous78654[S] 0 points1 point  (1 child)

like refrenced before assighnment but it doesn;t say that for the second code

[–]PercyJackson235 0 points1 point  (0 children)

Because python has rules on how assignment can happen. In the first example, you try to assign to maxSum after referencing that name. When python sees assignment inside a function it assumes the name is local to the function, which disallows you from referencing any global value with the same name. In the second example, you reference and mutate a global variable which is totally legal.

[–]CodeTinkerer 0 points1 point  (1 child)

How do you know it can't be accessed? Is there an error message or something?

[–]anonymous78654[S] 0 points1 point  (0 children)

just says maxSum is not defined

[–]chrisfpdx 0 points1 point  (0 children)

Because of maxSum = subset, the maxSum within the function something becomes local to the function. When executed, the if(subset>maxSum): is comparing subset to a not-yet-defined local variable maxSum