Ask Your Question

Passing arguments to callback in Python

asked 2016-04-09 06:29:08 -0600

steinaak gravatar image

updated 2016-04-09 07:42:45 -0600

gvdhoorn gravatar image


I have a problem passing arguments to a callback function when Subscribing a ROStopic. The code below is what I do have now. But I only received error messages when I tried to pass arguments to the callback function. I want to send two dictionaries to the callback function together with the text received on the topic called "text" in the subscriber function below. How to pass these arguments? So far I have only been receiving thousands of error messages.

def callback(data):

def subscriber():
    dict_1 = {.....}
    content = rospy.Subscriber("text",String,callback)

Hoping for good answers!


edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-04-09 08:06:14 -0600

lucasw gravatar image

updated 2016-04-09 08:12:00 -0600

You want to use the optional callback_args parameter to Subscriber:

def callback(data, args): 
  dict_1 = args[0]
  dict_2 = args[1]
sub = rospy.Subscriber("text", String, callback, (dict_1, dict_2))

Or you could put these into a class, and have the dictionaries be class member variables.

The C++ equivalent uses boost::bind

Cut and paste a reasonable amount of your error messages into the question body if you need help with errors.

edit flag offensive delete link more


THANK YOU SO MUCH!!!!! exactly what I wanted!

steinaak gravatar imagesteinaak ( 2016-04-09 08:42:46 -0600 )edit

@steinaak: why did you delete the question? If @lucasw has answered your question, please just accept the answer by ticking the checkmark to the left of the answer.

gvdhoorn gravatar imagegvdhoorn ( 2016-04-09 08:53:21 -0600 )edit

This is exactly what I was looking for, thanks a ton!!

Rodrigo gravatar imageRodrigo ( 2016-07-29 11:24:16 -0600 )edit

Would have been nice if @steinaak could've done it himself, but I've accepted the answer by @lucasw, as @steinaak (and @Rodrigo) has reported that it worked for him.

gvdhoorn gravatar imagegvdhoorn ( 2016-08-13 11:48:25 -0600 )edit

Could you please point to an example with class and members?

yossi_ov gravatar imageyossi_ov ( 2017-11-09 07:57:06 -0600 )edit

If I want to use this with message_filters.Subscriber() then how should I do it?

TrinhTUHH gravatar imageTrinhTUHH ( 2017-11-30 09:31:08 -0600 )edit

answered 2019-03-11 09:53:36 -0600

fbelmonteklein gravatar image

updated 2019-03-11 09:54:49 -0600

Additionally to the answer presented you can use a lambda like:

def callback(data, dict_1, dict_2):
callback_lambda = lambda x: callback(x,dict_1,dict_2)
sub = rospy.Subscriber("text", String, callback_lambda)

This was useful to me since the version of ROS I was using (hydro) did not seem to have callback_args (see ) implemented yet.

edit flag offensive delete link more


This a very elegant solution and also thanks for pointing out it solves the issue on hydro.

bruno.rosa gravatar imagebruno.rosa ( 2019-06-17 04:10:36 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2016-04-09 06:29:08 -0600

Seen: 13,519 times

Last updated: Mar 11