How to call another function from a function [duplicate] - python

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)

Related

return statement not working in python 3.6.3 while using functions [duplicate]

This question already has answers here:
How can I return two values from a function in Python?
(8 answers)
Closed 2 years ago.
It seems that return is not working:
def hello(x,y):
return x
return y
I can't seem to return y why is this happening. I am beginner so sorry if this seems dumb to you.
Instead of returning x and then y, how about you return them both? return x, y.
After a return statement you leave the function.
Here is how you would return 2 values and call them. Return x immediately exits the function and you won't return y.
def hello(x,y):
return x,y
x,y=hello(1,2)
print(x,y)

How to writing a function in Python to call another function while only assigning value to some arguments? [duplicate]

This question already has answers here:
Python Argument Binders
(7 answers)
Closed 3 years ago.
Or equivalently, to pass multiple arguments to a called function?
So I am trying to do:
define a function fun1
define another function fun2, which will call fun1 but only assigning some values to fun1, with other values being assigned otherwise; or maybe I can assign multiple arguments to a called function?
def fun1(x, y):
return x*y
def fun2(fun, x):
print(fun(x))
fun2(fun1(y=2), 3) # doesn't work
fun2(fun1, (3, 2)) # doesn't work either
So how should I do this? I want fun2 to just print fun1's result with some variable for x (here I choose it to be 3) and assigned(fixed) y=2
As suggested by #kaya3
Use python's variable *args feature.
IDLE :
>>> def fun1(x, y):
return x * y
>>> def fun2(fun, x):
print(fun(*x))
>>> fun2(fun1, (3, 2))
6

How to access closed over variables given only the closure function? [duplicate]

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

Is there any functional difference between return x,y and return (x,y)? Or is it purely syntactical? [duplicate]

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.

What is the lambda function doing in this code? [duplicate]

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]).

Categories