Answers for "ros2 add parameters to callback function"

1

ros2 add parameters to callback function

# 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
Posted by: Guest on March-01-2022

Code answers related to "ros2 add parameters to callback function"

Python Answers by Framework

Browse Popular Code Answers by Language