Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

pass function with parameters

# std::bind in python, or passing functions with paramaters can be done in two ways:
# with 'functools' or with lambda functions

# 1. functools: --------------------------
>>> from functools import partial

>>> def foobar(a, b):
...     return a+b

>>> p = partial(foobar, 1, 2)
>>> p()
3

>>> p2 = partial(foobar, 1)
>>> p2(2)
3

# 2. lambda: --------------------------

>>> def foobar(a,b):
...     return a+b
>>> foobar(1,2) # call normal function
3

>>> bind = lambda x: foobar(x, 2) # bind 2 to foobar
>>> bind(1) 
3

>>> bind = lambda: foobar(1,2) # bind all elements  
>>> bind()  
3
 
PREVIOUS NEXT
Tagged: #pass #function #parameters
ADD COMMENT
Topic
Name
1+8 =