0

The following snippet-code complains error saying that "unresolved reference maxFreq":

def returnItemsWithMinSupport():
    maxFreq = -1
    def recInFreqSet():
        if 2 > maxFreq:
            maxFreq = 1
        return 3

But if I change it to the following, it complains nothing:

def returnItemsWithMinSupport():
    maxFreq = -1
    def recInFreqSet():
        if 2 > maxFreq:
            x = 1
        return 3

Why is that? Great thanks.

5
  • @Vaulstein, It's not a global variable, but a variable in outer scope. Commented Nov 19, 2016 at 10:30
  • 2
    You cannot set outer variable in inner function. If you are using Python 3.x, you can declare the variable as nonlocal maxFreq allowing outer variable set. See nonlocal statemetn. Commented Nov 19, 2016 at 10:32
  • @falsetru: Oh sorry. But the first code runs for me Commented Nov 19, 2016 at 10:32
  • 2
    @Vaulstein, If you actually call returnItemsWithMinSupport(), you will see exception. (you need to modify the returnItemsWithMinSupport also to call recInFreqSet) Commented Nov 19, 2016 at 10:34
  • @falsetru Got it, thanks! Commented Nov 19, 2016 at 10:51

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.