This question already has answers here:
Can a variable number of arguments be passed to a function?
(6 answers)
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
(25 answers)
Closed 9 months ago.
I have a couple of functions defined like this (all parameters in a single array):
import numpy as np
def poly(x, params):
val = 0.
for i in range(len(params)):
val += params[-1 - i]*(x**i)
return val
a = 1.
b = 1.
c = 1.
params = [a, b, c]
Now I need to create the same function but without array as an argument of a function. I don't want to rewrite all my code, I need some new function for redefinition. The redefined function of my example should look like this:
def new_poly(x, a, b, c):
#take all parameters and put them back in an array to make the rest of the code work
params = [a, b, c]
val = 0.
for i in range(len(params)):
val += params[-1 - i]*(x**i)
return val
I will be grateful for any tips!
You can use *args:
def use_star_args(*args):
params = [*args]
print(params)
use_star_args(1, 2, 3)
Related
This question already has answers here:
Ignore python multiple return value
(12 answers)
Closed 1 year ago.
I have defined a function as:
def f():
a = 5
b = 6
c = 7
def g(x):
return x+2
return a, b , c, g
I would like to know how to get only one of the value returned, without the other ones. For example If I am only interested in c, is there an alternative to:
a, b, c, g = f()
To get c?
Python returns multiple values as a tuple. You can check this by doing
>>> type(f())
>>> <class 'tuple'>
As from your implementation we can assume that the c variable value always lies on the index 2 so you can do
c_value = f()[2]
This question already has answers here:
Python augmenting multiple variables inline
(2 answers)
Closed 2 years ago.
I'd like to do this:
def ret():
return 1, 1
a, b = 1, 1
a, b += ret()
print(a, b)
>>> 2, 2
And if you want to concentrate it even further, just:
a, b = 1, 1
a, b += 1, 1
Of course this gives an error:
SyntaxError: illegal expression for augmented assignment
Is there any native way to do this?
No, there is no syntactic sugar for this.
However, you can easily define a function to do this:
import operator
def multi_update(init, update, op=operator.iadd):
return (op(x, y) for x, y in zip(init, update))
a, b = 1, 1
a, b = multi_update((a, b), (1, 1))
print(a, b)
# 2 2
No, you cannot provide augmented assignment on multiple variables at once.
Please read this solution:
Python augmenting multiple variables inline
This question already has answers here:
Apply a list of Python functions in order elegantly
(2 answers)
Closed 3 years ago.
For some function f and variable a,
b_1 = f(a),
b_2 = f(b_1),
b_3 = f(b_2)
...
b_n = f(b_n-1)
I would like to do n-times by iterated method. In a functional way, this is accomplished by a function composition.
f...(f(f((a)))
You can achive your result by recusion
you can refer below pesudo code
#define f(a):
# do computation
# if terminating condition
# then return the computed result
#else
# return f(computed result)
This question already has answers here:
How to repeat a function n times
(7 answers)
Closed 4 years ago.
I am having trouble creating a higher order function in python that applies a function f, n times to generate a new function h, as its return value.
def compile(f, n)
# h is a function, f applied n times
...
return h
new = compile(lambda x: 2*x, 3)
new(4) == 32 # new(4) == 2(2(2(4)))
Since Python's functions are first-class citizens, a function can itself define a new function and return it. Defining a function inside a function can be done with def in the same way it would be in you top scope.
As a sidenote, I recommend you do not use compile as a function name as it does not accurately reflect what you are trying to do, which is called function composition, as well as overwrite the builtin function compile.
def compose_with_self(f, n):
def composed(arg):
for _ in range(n):
arg = f(arg)
return arg
return composed
Example:
def add_one(x):
return x + 1
add_three = compose_with_self(add_one, 3)
print(add_three(1)) # 4
You can do this easily using recursion
if n is zero, simply return x
otherwise, n is at least 1, apply f(x) to the recursive result compile (f, n - 1)
We can encode this in Python easily
def compile (f, n):
return lambda x: \
x if n is 0 else compile (f, n - 1) (f (x))
double_thrice = compile (lambda x: 2 * x, 3)
print (double_thrice (4))
# 32
This question already has answers here:
Pass a function as a variable with one input fixed
(4 answers)
Closed 5 years ago.
I have a function foo that takes 2 arguments a and b. I want to create a list of functions that are similar to foo, but value of a is fixed.
def foo(a,b):
return a*b
fooList = []
for i in range(n):
fooList.append(foo(i))
I want fooList[i] to return a function that is similar to foo(i, b).
You can use functools.partial:
from functools import partial
def foo(a, b):
return a * b
fooList = []
for i in range(n):
fooList.append(partial(foo, i))
Then you'd do fooList[i](5) to calculate i * 5.
You can also curry lambda functions, somewhat like this:
for i in range(n):
fooList.append((lambda x: lambda y: x * y)(i))
You can then call that the same way as above.