Call a function inside another function call [duplicate] - python

This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 6 years ago.
def func1(arg1,arg2,arg3,arg4)
...
def func2(arg5,arg6)
return a,b,c,d
func1(func2(arg5,arg6))
Can I call func1(func2(arg5,arg6)) like this??
as func2 will return 4 items that'll be passed to func1

You would have to unpack the arguments, but yes you can do that using the * operator.
func1(*func2(arg5,arg6))

Related

Using Iterator inside a lambda statement [duplicate]

This question already has answers here:
Lambda in a loop [duplicate]
(4 answers)
What do lambda function closures capture?
(7 answers)
Closed 2 years ago.
Why is the lambda expression only outputting the same result?
I expected to get the current value of the iterator and therefore the lambda expressions would output the numbers from 1 to 4 but that is obviously not the case.
I assume that the problem may have something to do with how the iterator is treated by the lambda expression but i could not find a answer to it yet. Thank you in advance
list = []
for i in range(5):
list.append(lambda:i)
for a in list:
print(a())
---OUTPUT---
4
4
4
4

python logical operator with variable number of arguments [duplicate]

This question already has answers here:
How do Python's any and all functions work?
(10 answers)
Closed 5 years ago.
is there a way in python to check the logical operator AND with variable number of arguments. for example:
def and_function(a,b,c,d,e....):
if a and b and c and d and c and d and e and . and . is True:
return True
i see that this is possible using *args function and using a counter. But wish to know if there is a better way of doing it.
You basically want all() but to customize it you can use this:
def and_function(*args):
return all([a > 5 for a in args])

Dynamic arguments for Python functions [duplicate]

This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 7 years ago.
I have a list of lists, and I would like to enter them into a function in such a way that each element of the main list is a different argument to the function.
squares = [[1,5,9,13], [2,6,10,14], [3,7,11,15], [4,8,12,16]]
print zip(squares[0], squares[1], squares[2], squares[3])
# displays [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
Is there a way to do something like print zip(arguments(squares)) so that the arguments can be entered dynamically?
This is what the * operator is for:
print zip(*squares)

Can I create a function to clear variables in python ? [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
call-by-reference function parameters
(4 answers)
Immutable vs Mutable types
(18 answers)
Closed 9 years ago.
Is it possible in python to do something like this:
def number_clear(num):
num = 0
x = 10
number_clear(x)
print x
>>0
In C we could do that simply by pointers

Calling itertools.product with unkown number of args [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 9 years ago.
I hope this is a easy one.
Is there any possiblity to call itertools.product with a yet (not hardcoded) not knwon number of arguments?
Something like this:
itertools.product(x[0],x[1],x[2],....)
and the dimension of x can't be hardcoded
Thanks!
Try:
itertools.product(*x)
i.e. we unpack the argument list.
You can use
itertools.product(*x)
Lookup *args and **kwargs?
a = [1,2,3]
b = [2,3,4]
c= [a,b]
itertools.product(*c)
You can pass array of arguments using *

Categories