Why does rospy instantiate msg arrays as lists but deserialize them as tuples?
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?