python bound on delay [lambda x: x * i for i in range (4)] be appreciated

About [lambda x: x * i for i in range (4)] be appreciated

 

topic:

lst = [lambda x: x*i for i in range(4)]
res = [m(2) for m in lst]
print res

Actual Output: [6, 6, 6, 6]

Want output [0, 2, 4, 6] should be how to change? as follows:

lst = [lambda x, i=i: x*i for i in range(4)]
res = [m(2) for m in lst]
print res

This question relates to the knowledge (Python scopes) Python closures and delay bound.

In the core Python programming, the closure defined as follows:

If an internal function, the external scope (but not in the global scope) variables are referenced, then the internal function was recognized as a closure.

Summed up in three points:

1, is an inline function

2, variable references to external functions

3, the external function returns the built-in functions

Simple closures example:

Copy the code
def counter(start_at=0):
    count = [start_at]
    def incr():
        count[0] += 1
        return count[0]
    return incr
Copy the code

Above that question can be written like this:

Copy the code
def func():
    fun_list = []
    for i in range(4):
        def foo(x):
            return x*i
        fun_list.append(foo)
    return fun_list
for m in func():
  print m(2)
Copy the code

FUNC () is a list containing four functions:

[<function func at 0x00000000021CE9E8>, <function func at 0x00000000021CEA58>, <function func at 0x00000000021CEAC8>, <function func at 0x00000000021CEB38>]

When we performed m (2), runs to foo () function inside, found variables () in the variable i is not foo, so they function func to find an external variable i, but this time for the external loop has been completed, the last i = 3. So, every time

Performed m (2), the value of i is 3, and therefore, the end result would be [6, 6, 6, 6].

When the addition i = i in foo (), namely:

Copy the code
def func():
    fun_list = []
    for i in range(4):
        def foo(x, i=i):
            return x*i
        fun_list.append(foo)
    return fun_list
for m in func():
  print m(2)
Copy the code

In this case, when the for loop is executed, has the i (0, 1, 2, 3) the value is passed to the foo () function, then the internal variable i is already foo () function, the operation foo () function, the function will not be looking to external variables i, run directly

x * i (0, 1, 2, 3), so the final result would be [0, 2, 4, 6].

 

Reference blog: playfully paramecium    https://www.cnblogs.com/delav/

topic:

lst = [lambda x: x*i for i in range(4)]
res = [m(2) for m in lst]
print res

Actual Output: [6, 6, 6, 6]

Want output [0, 2, 4, 6] should be how to change? as follows:

lst = [lambda x, i=i: x*i for i in range(4)]
res = [m(2) for m in lst]
print res

This question relates to the knowledge (Python scopes) Python closures and delay bound.

In the core Python programming, the closure defined as follows:

If an internal function, the external scope (but not in the global scope) variables are referenced, then the internal function was recognized as a closure.

Summed up in three points:

1, is an inline function

2, variable references to external functions

3, the external function returns the built-in functions

Simple closures example:

Copy the code
def counter(start_at=0):
    count = [start_at]
    def incr():
        count[0] += 1
        return count[0]
    return incr
Copy the code

Above that question can be written like this:

Copy the code
def func():
    fun_list = []
    for i in range(4):
        def foo(x):
            return x*i
        fun_list.append(foo)
    return fun_list
for m in func():
  print m(2)
Copy the code

FUNC () is a list containing four functions:

[<function func at 0x00000000021CE9E8>, <function func at 0x00000000021CEA58>, <function func at 0x00000000021CEAC8>, <function func at 0x00000000021CEB38>]

When we performed m (2), runs to foo () function inside, found variables () in the variable i is not foo, so they function func to find an external variable i, but this time for the external loop has been completed, the last i = 3. So, every time

Performed m (2), the value of i is 3, and therefore, the end result would be [6, 6, 6, 6].

When the addition i = i in foo (), namely:

Copy the code
def func():
    fun_list = []
    for i in range(4):
        def foo(x, i=i):
            return x*i
        fun_list.append(foo)
    return fun_list
for m in func():
  print m(2)
Copy the code

In this case, when the for loop is executed, has the i (0, 1, 2, 3) the value is passed to the foo () function, then the internal variable i is already foo () function, the operation foo () function, the function will not be looking to external variables i, run directly

x * i (0, 1, 2, 3), so the final result would be [0, 2, 4, 6].

 

Reference blog: playfully paramecium    https://www.cnblogs.com/delav/

Guess you like

Origin www.cnblogs.com/yafeng666/p/12146711.html