This question already has answers here:
Reflect / Inspect closed-over variables in Python
(3 answers)
Closed 3 years ago.
In the following example:
def speak(volume):
def whisper(text):
print(text.lower() + ('.' * volume))
def yell(text):
print (text.upper() + ('!' * volume))
if volume > 1:
return yell
elif volume <= 1:
return whisper
func = speak(volume=10)
func('hello')
HELLO!!!!!!!!!! # <== obviously `10` is stored in `func` somewhere
Given func, how would I get the "volume"? Is there something within the func namespace which gives the value of 10? I thought perhaps it would be in func.__globals__ or func.__dict__ but it's in neither.
Below (the code below return 10)
func.__closure__[0].cell_contents
Related
This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 2 years ago.
x = 10
def double(y):
return 2 * x
print(double(x))
Output is 20
As far as I know, it should return None because in function "double" I double x which is undefined that block.
x is defined outside of the function scope as a global variable that is also available in the function
It is because x is a global variable, so when you call double it is just multiplying x by 2 no matter what
if you were to say put
x = 10
def double(y):
return 2 * x
print(double(40))
you would still get 20 as you are returning the variable x multiplied by 2.
This question already has answers here:
How do you call a function in a function?
(4 answers)
Closed 2 years ago.
My First function is addition of program
def addn(x,y):
return (x + y)
My second function is output got from the first function + 2
def addn2(x,y)
return (x+y+2)
Instead of above searching for anything like below, with out re writing everything in the second function
def addn3(x,y):
return (addn + 2)
The parameters are important:
def addn3(x, y):
return (addn(x, y) + 2)
This question already has answers here:
When are parentheses required around a tuple?
(3 answers)
Closed 3 years ago.
So, for example, is there any actual difference between:
def test1():
x = 1
y = 2
return x,y
And:
def test2():
x = 1
y = 2
return (x,y)
Both are valid constructors of the builtin tuple type, so, yes, the difference is purely a different syntax for the same thing.
This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 2 years ago.
My recrusive function returns None even when I print the variable on the above line
When I call the function it prints exactly what I want, but returns None!
def nRound(vector, root):
tempRoot = root
a = vector.pop()
b = vector.pop()
if a+b < 1.0:
vector.append(a+b)
rootn = Node(a+b)
rootn.right = tempRoot
rootn.left = Node(b)
nRound(vector, rootn)
else:
rootn = Node(a+b)
rootn.right = tempRoot
rootn.left = Node(b)
print(rootn)
return rootn
I don't understand why it returns None instead rootn. Thanks in advance.
Your function is recursive, and only the base case returns a value. Values from the recursive call are not passed upward:
nRound(vector, rootn)
That means an external caller will only get a value if the function immediately reaches the base case. The above line should be
return nRound(vector, rootn)
This question already has answers here:
python max function using 'key' and lambda expression
(6 answers)
Closed 6 years ago.
I have the following code for a greedy implementation of the traveling salesman problem. I can't wrap my head around what exactly the lambda function does in this code.
def distance(p1, p2):
return ((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2) ** 0.5
def tsp_greedy(points, start=None):
if start is None:
start = points[0]
to_visit = points
path = [start]
to_visit.remove(start)
while to_visit:
closest = min(to_visit, key=lambda x: distance(path[-1], x))
path.append(closest)
to_visit.remove(closest)
return path
I realize it's creating an anonymous function which x gets passed into. But I'm not sure what is getting passed into this function. What is x?
closest becomes to_visit[i] such that
distance(path[-1], to_visit[i]) =
min(distance(path[-1], to_visit[0]), distance(path[-1], to_visit[1]), ...)
In other words, the lambda function makes comparison not by to_visit[i] but by distance(path[-1], to_visit[i]).