ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

TypeError: las_callback() takes exactly 2 arguments (1 given)

asked 2020-12-12 21:58:27 -0600

zzdmw gravatar image

I try to make spatial median filtering that uses in sensor_msgs/LaserScan.msg. this is my python code:

#!/usr/bin/env python

import rospy

from sensor_msgs.msg import LaserScan

def las_callback(self, msg):




    for i in range(0, len(data_list)):

        if (i>= self.median_filter_size and i<(len(data_list)-self.median_filter_size)):




sub = rospy.Subscriber('/base_scan', LaserScan, las_callback)

when I try to rosrun this code, it shows me a worrying message is:

[ERROR] [1607831425.437657, 1678.699000]: bad callback: <function las_callback at 0x7fb50adbddd0>
Traceback (most recent call last):
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/", line 750, in _invoke_callback
TypeError: las_callback() takes exactly 2 arguments (1 given)

How can I solve this problem? thanks for your help!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-12-13 01:48:40 -0600

gvdhoorn gravatar image

updated 2020-12-13 02:24:20 -0600

This is a Python problem more than a ROS one.

def las_callback(self, msg):

You've defined a method here (the first argument is self), but it is not part of any class.

Remove the self argument, so las_callback(..) becomes a function.

Alternatively: create a Python class and make las_callback(..) a method of that class. You'll have to take care to create all the member variables as well in that case, or the callback will not run successfully.

edit flag offensive delete link more


when I make"las_callback(self, msg):" become "def las_callback(msg):". It show a new error:

[ERROR] [1607846695.003072, 183.190000]: bad callback: <function las_callback at 0x7f8cda22ed50>
Traceback (most recent call last):
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/", line 750, in _invoke_callback
  File "/home/wg/catkin_ws/src/rob599_hw3/src/", line 13, in las_callback
NameError: global name 'self' is not defined
zzdmw gravatar image zzdmw  ( 2020-12-13 02:06:07 -0600 )edit

That's because you are still referencing self in the body of your callback.

This is still a Python problem: did you copy the callback code from somewhere?

If you haven't yet, you might want to lookup some Python tutorials to get some more experience with Python before taking on ROS. Learning all these new things at the same time can be quite a challenge.

gvdhoorn gravatar image gvdhoorn  ( 2020-12-13 02:23:24 -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

1 follower


Asked: 2020-12-12 21:58:27 -0600

Seen: 929 times

Last updated: Dec 13 '20