0
$\begingroup$

Rosanswers logo

According to the ROS wiki page for msg,

In rospy, arrays are deserialized as tuples for performance reasons, but you can set fields to tuples and lists interchangeably.

I can't complain about the interchangability, but I'd expect that if msg arrays are deserialized as tuples, they would always be tuples unless the programmer explicity sets them to lists. However, I've observed that when instantiating a rospy msg using its empty constructor, any array fields are instantiated as empty lists. For example:

>>> from visualization_msgs.msg import Marker
>>> m = Marker()
>>> m.points
[]

This inconsistency means it's harder to write methods that deal with msg array fields because you have to expect the field to sometimes be an array and sometimes be a tuple. For example:

# list version -- only works if you know your marker was created by calling the constructor
def add_point_to_marker(marker, point):
    marker.points.append(point)

# tuple version -- only works if you know your marker was created by deserialization
def add_point_to_marker(marker, point):
    marker.points += point

# generic version
def add_point_to_marker(marker, point):
    marker.points = list(marker.points) + [point]

A method that naively assumes list or tuple will work perfectly until the time you pass it the other. Then it's a long and painful bug hunt.

Just wondering, am I missing something here? Why was it designed like this? Is it a bug or a feature?


Originally posted by dinosaur on ROS Answers with karma: 233 on 2015-09-17

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Arrays are easier to work with when constructing messages, since they can be dynamically appended to. Hence the use of arrays for newly created, empty messages.

Tuples are faster because they're a fixed size, but they can't be resized once created. Hence the use of tuples in the deserializer, where all of the array sizes are already known.


Originally posted by ahendrix with karma: 47576 on 2015-09-18

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by gvdhoorn on 2015-09-18:
Not a Python expert, but could it also be the author(s) of rospy tried to convey the fact that on the receiving side msgs aren't really supposed to be changed? The immutability of tuples would seem to be a way to reflect that.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.