[Python] How to check if an object is a ROS message

Discussão em 'Python' iniciado por Stack, Agosto 6, 2025.

  1. Stack

    Stack Membro Participativo

    In ROS1, it was possible to check if an object was a ROS Message by using genpy:

    isinstance(obj, genpy.message.Message)


    However, I cannot seem to find the same functionality in ROS2. If we look at the __mro__ of the classes there does not seem to be anything to work with:

    In ROS1:

    >>> from rospy_tutorials.srv import AddTwoIntsRequest
    >>> AddTwoIntsRequest.__mro__
    (<class 'rospy_tutorials.srv._AddTwoInts.AddTwoIntsRequest'>, <class 'genpy.message.Message'>, <class 'object'>)


    In ROS2:

    >>> from example_interfaces.srv import AddTwoInts
    >>> AddTwoInts.Request.__mro__
    (<class 'example_interfaces.srv._add_two_ints.AddTwoInts_Request'>, <class 'object'>)


    Have I missed something?

    Continue reading...

Compartilhe esta Página