# ROS 2 Foxy 自定义消息属性无法访问

> 原文链接：https://robotics.hellobike.com/pitfalls/2023-08-11-robotics-se-25134
> 频道：pitfalls | 更新日期：2023-08-11 | 标签：auto, stackexchange, python, ros2, ros-foxy
> 核心摘要：在 Python 中把消息类当实例使用导致 header.stamp 报错，改为调用构造函数实例化即可。

# ROS 2 Foxy 自定义消息属性无法访问

::: v-pre
> 本文由 AI 根据公开内容整理，可能有误，请以原文为准：[ros2 foxy python or interface not seeing full extent of the custom message](https://robotics.stackexchange.com/questions/25134/ros2-foxy-python-or-interface-not-seeing-full-extent-of-the-custom-message)（Robotics Stack Exchange #25134）

**状态**：已解决

## 问题

ROS 2 Foxy 中自定义消息 `Logitechjoy` 可构建并导入 Python，但只能访问 `header`、`buttons`，访问 `header.stamp` 或 `buttons.green` 报 `AttributeError: 'property' object has no attribute 'stamp'`。

## 环境

ROS 2 Foxy；Python；`colcon build`；自定义消息包 `control_main_msg`。

## 解决办法

1. 将 Python 代码中的 `joy_msg = Logitechjoy` 改为 `joy_msg = Logitechjoy()`，创建消息类实例后再访问 `joy_msg.header.stamp` 等字段。
2. 如果类似错误由构建中断引起，可删除 `build` 目录后重新构建。

原问题作者：Frenki221；采纳答案作者：cst0。原内容依 CC BY-SA 4.0 授权，本页摘要同样依 CC BY-SA 4.0 授权。
:::

## 原始参考来源 (Sources)

- [Robotics Stack Exchange #25134：ros2 foxy python or interface not seeing full extent of the custom message](https://robotics.stackexchange.com/questions/25134/ros2-foxy-python-or-interface-not-seeing-full-extent-of-the-custom-message)