'closures with two inner functions python

I am trying to write a closure with two inner functions, but I am getting the below error

def factory(n=0):
#n=0

    def current():
       return n
    return current
    def counter():
        n=n+1
        return n
    return counter


  f_current,f_counter = int(input())

  print(f_counter())
  print(f_current())

I have the below error:

   >>4
   Traceback (most recent call last):
   File "C:/Users/lokesh/Desktop/python/closure3.py", 
    line 13, in <module>
   f_current,f_counter = int(input())
   TypeError: 'int' object is not iterable

My requirement is after giving input 4,it should display:

  4
  5

I am new to python, can somebody help me here... thanks in advance



Solution 1:[1]

That looks more like what you want:

def factory(n=0):

    def current():
        return n

    def counter():
        nonlocal n
        n += 1
        return n

    return current, counter

f_current, f_counter = factory()

print(f_current())
print(f_counter())
print(f_current())
print(f_counter())

Output:

0
1
1
2

With 4 as input:

f_current, f_counter = factory(4)
print(f_current())
print(f_counter())

4
5

factory() returns both inner functions. You need to use nonlocal to increment the n form the enclosing function. Without nonlocal you would not be able to modify n but would get:

UnboundLocalError: local variable 'n' referenced before assignment

because n is just a local variable. nonlocal n makes n from the enclosing function modifiable inside the inner function. Assessing n in current is fine, because Python's scoping rules allow read access to variables form an outer scope, here from the scope of the enclosing function.

Solution 2:[2]

Hi Mike ,I think non-local does'nt make any sense,I am giving the solution which worked in my case.

def factory(n):

    def current():

        return n

    def counter():

        n=int(current())
        n=n+1
        return n

    return current, counter
n=0
f_current, f_counter = factory((input()))

print(f_current())
print(f_counter())

Solution 3:[3]

def factory(n=0):

    def current():
        return n

    def counter():
        return n+1

    return current, counter

f_current, f_counter = factory(int(input()))

print(f_current())
print(f_counter())

Solution 4:[4]

Mike's Nonlocal usage is very nice,but we can also access the variable value of n from enclosing scope by a new variable m inside Counter inner function and return it after incrementing.In that way,Nonlocal is not needed.

        def factory(n=0):
            def current():
                return n

            def counter():
                 m=n
                 m=m+1
                 return m

            return current,counter

         f_current,f_counter=factory(int(input()))

         print(f_current())
         print(f_counter())

`

Solution 5:[5]

You can simply use a new variable as the argument for the inner function which is equal to that of the outer function and proceed.

def factory(n=0):
    def current():
        return n

    def counter(x=n):
        return x+1

    return current, counter

f_current,f_counter =factory(int(input()))

print(f_current())

print(f_counter())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ayushi tripathi
Solution 3 Gryphon
Solution 4
Solution 5 BrokenBenchmark