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.

smci
  • 32,567
  • 20
  • 113
  • 146
KAs
  • 1,818
  • 4
  • 19
  • 37
  • @Vaulstein, It's not a global variable, but a variable in outer scope. – falsetru Nov 19 '16 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](https://docs.python.org/3/reference/simple_stmts.html#the-nonlocal-statement). – falsetru Nov 19 '16 at 10:32
  • @falsetru: Oh sorry. But the first code runs for me – Vaulstein Nov 19 '16 at 10:32
  • 2
    @Vaulstein, If you actually call `returnItemsWithMinSupport()`, you will see exception. (you need to modify the `returnItemsWithMinSupport` also to call `recInFreqSet`) – falsetru Nov 19 '16 at 10:34
  • @falsetru Got it, thanks! – Vaulstein Nov 19 '16 at 10:51

0 Answers0