【问题标题】:How to access class member array in python如何在python中访问类成员数组
【发布时间】:2020-12-06 23:26:59
【问题描述】:

我有一个班级成员。如何访问远程传感器温度的“值”?这是 pyecobee 模块对 ECOBEE 恒温器的响应。

我试过thermostat_response.thermostatList,但它给了我错误“AttributeError:'EcobeeThermostatResponse'对象没有属性'thermostatList'”

我可以访问thermostat_response.page 和thermostat_response.status,但不能访问thermostat_response.thermostatList

thermostat_response = EcobeeThermostatResponse(
  page=Page(
    page=1,
    pageSize=1,
    total=1,
    totalPages=1
  ),
  status=Status(
    code=0,
    message=
  ),
  thermostatList=[
    Thermostat(
      alerts=None,
      modelNumber=athenaSmart,
      reminders=None,
      remoteSensors=[
        RemoteSensor(
          capability=[
            RemoteSensorCapability(
              id=1,
              type=temperature,
              value=717
            ),
            RemoteSensorCapability(
              id=2,
              type=occupancy,
              value=true
            )
          ],
          code=EDGJ,
          id=rs:100,
          inUse=False,
          name=Bedroom,
          type=ecobee3_remote_sensor
        ),
        RemoteSensor(
          capability=[
            RemoteSensorCapability(
              id=1,
              type=temperature,
              value=696
            ),
            RemoteSensorCapability(
              id=2,
              type=humidity,
              value=62
            ),
            RemoteSensorCapability(
              id=3,
              type=occupancy,
              value=true
            )
          ],
          code=None,
          id=ei:0,
          inUse=True,
          name=Living Room,
          type=thermostat
        )
      ],
      runtime=None,
      weather=None
    )
  ]
)

这是EcobeeThermostatResponse类的定义

class EcobeeThermostatResponse(EcobeeStatusResponse):
    __slots__ = ['_page', '_thermostat_list']

    attribute_name_map = {'page': 'page', 'thermostat_list': 'thermostatList',
                          'thermostatList': 'thermostat_list', 'status': 'status'}

    attribute_type_map = {'page': 'Page', 'thermostat_list': 'List[Thermostat]', 'status': 'Status'}

    def __init__(self, page, thermostat_list, status):
        """
        Construct a EcobeeThermostatResponse instance

        :param page: The page information for the response
        :param thermostat_list: The list of thermostats returned by the request
        :param status: The api response code
        """
        self._page = page
        self._thermostat_list = thermostat_list
        EcobeeStatusResponse.__init__(self, status)

    @property
    def page(self):
        """
        Gets the page attribute of this EcobeeThermostatResponse instance.

        :return: The value of the page attribute of this EcobeeThermostatResponse instance.
        :rtype: Page
        """
        return self._page

    @property
    def thermostat_list(self):
        """
        Gets the thermostat_list attribute of this EcobeeThermostatResponse instance.

        :return: The value of the thermostat_list attribute of this EcobeeThermostatResponse instance.
        :rtype: List[Thermostat]
        """
        return self._thermostat_list

【问题讨论】:

  • 我会尝试进入 EcobeeThermostatResponse 类,看看它的属性是什么,这样你就知道如何访问它们了。
  • 另外,我不确定“message=”是否有问题
  • thermostat_response = 我正在执行的某个函数,这是我得到的响应。现在我试图从这个变量中获取温度。 EcobeeThermostatResponse 是一个类。

标签: python ecobee


【解决方案1】:

根据documentation,您可以使用thermostat_response.thermostat_list()(作为函数)。


如果您没有文档怎么办?

如果您遇到类似的问题并且没有文档,您可以使用 Python 的 dir() 输出对象的所有属性,例如在你的情况下:

`dir(thermostat_response)`

this SO question 中的更多信息。

【讨论】:

  • 它说 vars() 必须具有 dict 属性。 thermostat_response.thermostatList() 也给出了同样的错误 AttributeError: 'EcobeeThermostatResponse' object has no attribute 'thermostatList'"
  • @HardyShah 这是thermostat_list(),而不是thermostatList()
  • @HardyShah 以及使用dir(thermostat_response) 怎么样? (vars 的替代品)
  • 那行得通。 thermostat_list() 和 dir(thermostat_response) 都有效。非常感谢您的帮助。
  • 很高兴这有帮助!答案编辑为提及dir 而不是vars
猜你喜欢
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-16
  • 2011-03-26
  • 2016-05-21
相关资源
最近更新 更多