Python类属性错误AttributeError:无法设置属性

时间:2017-02-27 08:32:49

标签: python python-2.7 properties setter attributeerror

class DockerEngine(Device):

  def __init__(self):
      super(DockerInfo, self).__init__()
      self.docker_id = None
      self.host_ip_address = None
      self.total_containers = 0
      self.running_containers = 0
      self.paused_containers = 0
      self.stopped_containers = 0

  @property
  def host_ip_address(self):
      return self._host_ip_address

  @host_ip_address.setter
  def host_it_address(self, ip):
      self._host_ip_address = ip

  @property
  def docker_id(self):
      return self._docker_id

  @docker_id.setter
  def docker_id(self, id):
      self._docker_id = id

初始化DockerEngine对象时,它会在__init__ self.host_ip_address中抱怨,无法设置属性。

1 个答案:

答案 0 :(得分:4)

您的代码有拼写错误

host_i t _address更改为host_i p _address

@host_ip_address.setter
def host_it_address(self, ip): <--- WRONG FUNCTION NAME
    self._host_ip_address = ip