This question already has answers here:
What do lambda function closures capture?
(7 answers)
Local variables in nested functions
(4 answers)
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Closed 4 years ago.
I found a interesting behavior of lambda function in Python.
If I do,
lambda_funcs = []
for i in range(10):
lambda_func = lambda x:x[i]
lambda_funcs.append(lambda_func)
X = ['a', 'b', 'c']
print lambda_funcs[0](X)
It throws an error: IndexError: list index out of range.
This code aims to create 10 lambda functions each of which returns i-th element. But, all lambda_func in the above code attempts to retrieve 10th element.
To make it work, I found that the following definition is a right way:
lambda_func = (lambda j: lambda x: x[j])(i)
Can anyone tell me what is going on inside lambda evaluation in Python?
Related
This question already has answers here:
Local variables in nested functions
(4 answers)
Lambda in a loop [duplicate]
(4 answers)
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Closed 2 years ago.
I want to generate the Lagrange basis polynomials for a set of points where T is the set of points and n is the amount of given points. What I did is ci = [lambda t: np.prod([(t - T[j]) / (T[i] - T[j]) for j in range(n) if j != i] for i in range(n)], however, the resulting lambdas are all the same.
I checked by printing [c(val) for c in ci] in the console for several different values and it all returns the same value. How is this possible, when clearly the formula I put in should return lambdas with a different structure for each i?
This question already has answers here:
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Python Argument Binders
(7 answers)
Closed 2 years ago.
The following Python program:
d = {}
for x in range(3):
d[x] = lambda: x
print(d[0](), d[1](), d[2]())
outputs:
2 2 2
x is bound by reference in the lambda expression. After the for statement, x is bound to 2, which explains the output.
I would like x to be bound by value instead, to get the following output:
0 1 2
How can I achieve this?
This question already has answers here:
What's the best way to return multiple values from a function? [duplicate]
(6 answers)
Alternatives for returning multiple values from a Python function [closed]
(14 answers)
Closed 2 years ago.
So i have a function that has to return 3 values, i haven't found a better way to do this other than returning a list. Is this code a good programming practice? And if not how to fix it.
Example function:
def func():
#code
return [a,b,c]
Main code:
#code
list = func()
k = list[0]
l = list[1]
m = list[2]
You can pack/unpack directly in python:
def func():
a = 1
b = 2
c = 3
return a, b, c
k, l, m = func()
This question already has answers here:
What is the difference between Python's list methods append and extend?
(20 answers)
Why does += behave unexpectedly on lists?
(9 answers)
Closed 4 years ago.
For Python list, is append() the same as +=?
I know that + will lead to the creation of a new list, while append() just append new stuff to the old list.
But will += be optimized to be more similar to append()? since they do the same thing.
It's an __iadd__ operator. Docs.
Importantly, this means that it only tries to append. "For instance, if x is an instance of a class with an __iadd__() method, x += y is equivalent to x = x.__iadd__(y) . Otherwise, x.__add__(y) and y.__radd__(x) are considered, as with the evaluation of x + y."
This thread specifically deals with lists and their iadd behavior
This question already has answers here:
Explanation of how nested list comprehension works?
(11 answers)
Closed 7 years ago.
def maxXor( l, r):
return max(a^b for b in range(a,r+1) for a in range(l,r+1))
The error shows NameError: global name 'a' is not defined.
I think the error is range(a,r+1)
Here I want the 'a' in range(a,r+1) reference 'a' in the inner loop for a in range(l,r+1)).
def maxXor( l, r):
return max(a^b for a in range(l,r+1) for b in range(a,r+1))