This question already has answers here:
Python lambda function printing <function <lambda> at 0x7fcbbc740668> instead of value
(5 answers)
Closed 7 months ago.
when i run the code i get a memory location as an output
<function at 0x00000200FF8A5E50>
val = (lambda colmns:(colmns.max + colmns.min)/2
print(val)
A lambda is just an anonymous function object and that's why it prints that way. In fact, as soon as you assign it to a variable... then why have a lambda in the first place? Might as well do
def foo(colmns):
return (colmns.max + colmns.min)/2
And then it wouldn't be too surprising that print(foo) is <function 0x...>.
Related
This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 13 days ago.
This snippet of python code
def changeValue(x):
x = 2*x
a = [1]
changeValue(a[0])
print(a[0])
Will print the result "1" when I would like it to print the result "2". Can this be done by only altering the function and no other part of the code? I know there are solutions like
def changeValue(x):
x[0] = 2*x[0]
a = [1]
changeValue(a)
or
def changeValue(x):
return 2*x
a = [1]
a[0] = changeValue(a[0])
I am wondering if the argument passed as-is can be treated as a pointer in some sense.
[edit] - Just found a relevant question here so this is likely a duplicate that can be closed.
No it's not possible. If you pass a[0], it's an int and it can't be mutated in any way.
If the int was directly in the global namespace, you could use global keyword. But it's not. So again no.
This question already has answers here:
Can Python's map function call object member functions?
(5 answers)
Closed 7 months ago.
I want to split the string using different characters("."&" ",").I didn't know there is a module called re so I did it in a very stupid way:
text = ... HELLO WO,RLD, this
l2 = text.split(" ")
list(map(str.split(","), l2))
And it turned out:
'list' object is not callable`.
I am sure I didn't define a variable or function called list .It ran successfully after I rewrite list(map(str.split(","),l2)) as list(map(lambda x:x.split(","),l2)).
Now I know I can use re.split(" |,",text), but I still wanna know why the error is happened on list.
map expects callable argument as first parameter str.split can be invoked
x = str.split
x()
while str.split(",") return a list, that's the cause of error.
You can use lambda to give map a callable function
list(map(lambda x: x.split(","), l2))
# [['...'], ['HELLO'], ['WO', 'RLD', ''], ['this']]
This question already has answers here:
What do lambda function closures capture?
(7 answers)
Closed 3 years ago.
The following is a code snippet which I don't seem to get. The question is how to make the function output the desired result (not mentioning what the desired result is, I assume its printing 0 to 9).
Here is the question:
What does the below code snippet print out? How can we fix the anonymous functions to behave as we'd expect?
functions = []
for i in range(10):
functions.append(lambda : i)
for f in functions:
print(f())
In Python, no new scope will be produced in for loop
So after for i in range(10), the variable i is still exist, and its value == 9. And the lambda function lambda : i access the variable i
In order to output your desired result, you should pass the variable as a function argument in loop
functions = []
for i in range(10):
functions.append(lambda i=i: i)
for f in functions:
print(f())
This question already has answers here:
What does the "yield" keyword do in Python?
(51 answers)
Closed 6 years ago.
**def colour_generator(lst):
for i in lst:
yield i
print()
colours=['red','green','blue']
print(colour_generator(colours))**
here the funtion colour_generetor returns the object address
but if in the same code we replace 'yield' with 'return' then it
returns the first element in the list(lst)..
Is 'yield' completely different from 'return' statement ?
Moreover **is it really possible to create object of a function?**As the output when 'return' statement is used instead of 'yield' statement in the above code is :"" or it is something else ?
Your description of observed behaviour shows that they are in fact different - the former returning a generator, the latter a value.
is it really possible to create object of a function?
A function is an object of type/class function:
# Python 2
>>> f = lambda x: x
>>> type(f)
<type 'function'>
>>> def g(): pass
>>> type(g)
<type 'function'>
# Python 3
>>> f = lambda x: x
>>> type(f)
<class 'function'>
This question already has answers here:
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
What do lambda function closures capture?
(7 answers)
Closed 9 years ago.
What is the difference between
for i in range(0,3): print cons[i]['fun'](x0)
and
for f in cons: print f['fun'](x0)
where cons is defined as following
A = np.asmatrix([[1,0,0],[0,1,0],[0,0,1]])
x0 = np.asarray([1,2,0])
cons = list()
for i in range(0,3): cons.append({'fun':lambda x: np.dot(A[i],x)})
Your problem probably is related to the fact that you are having a lambda clause using an unbound variable (i). Change your code like this:
for i in range(0,3): cons.append({'fun':lambda x, i=i: np.dot(A[i],x)})
(I. e. just insert that , i=i.)
This way the value of i is part of the lambda clause and not taken from the surrounding scope.