Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: passing a function with parameters as parameter [duplicate]

def lite(a,b,c):     #...  def big(func): # func = callable()     #...   #main big(lite(1,2,3)) 

how to do this?
in what way to pass function with parameters to another function?

like image 297
Mike Avatar asked Feb 27 '10 13:02

Mike


People also ask

Does Python pass-by-reference or copy?

All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function.

What are the 2 ways of passing parameters to functions?

There are two ways to pass parameters in C: Pass by Value, Pass by Reference.

Can you pass a function as a parameter to another function in Python?

Yes it is, just use the name of the method, as you have written. Methods and functions are objects in Python, just like anything else, and you can pass them around the way you do variables.

Can a function have multiple parameters Python?

Luckily, you can write functions that take in more than one parameter by defining as many parameters as needed, for example: def function_name(data_1, data_2):


2 Answers

Why not do:

big(lite, (1, 2, 3)) 

?

Then you can do:

def big(func, args):     func(*args) 
like image 152
Skilldrick Avatar answered Oct 05 '22 08:10

Skilldrick


import functools  #main big(functools.partial(lite, 1,2,3)) 
like image 33
Teddy Avatar answered Oct 05 '22 09:10

Teddy