简体   繁体   English

具有两个内部函数的闭包 python

[英]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 之后,它应该显示:

  4
  5

I am new to python, can somebody help me here... thanks in advance我是 python 的新手,有人可以帮我吗...在此先感谢

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:4作为输入:

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

4
5

factory() returns both inner functions. factory()返回两个内部函数。 You need to use nonlocal to increment the n form the enclosing function.您需要使用nonlocal来递增n形成封闭函数。 Without nonlocal you would not be able to modify n but would get:没有nonlocal你将无法修改n但会得到:

UnboundLocalError: local variable 'n' referenced before assignment

because n is just a local variable.因为n只是一个局部变量。 nonlocal n makes n from the enclosing function modifiable inside the inner function. nonlocal n使n来自封闭函数,可在内部函数内部修改。 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.current中评估n很好,因为 Python 的作用域规则允许从外部作用域读取变量,这里是从封闭函数的作用域。

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())
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())

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. Mike的Nonlocal用法很好,但是我们也可以通过Counter内部函数内部的一个新变量m从封闭作用域访问n的变量值,递增后返回。这样就不需要Nonlocal了。

        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())

` `

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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM