【问题标题】:"Object not callable" error if I specify a parameter when trying to retrieve value如果我在尝试检索值时指定参数,则会出现“对象不可调用”错误
【发布时间】:2020-02-11 02:43:20
【问题描述】:

当我尝试检索属性的值并包含参数时,我收到“对象不可调用”错误消息。

当我获得该值时,我想选择将“刷新”指定为 True,以强制从我的主机重新学习该值,而不是使用存储在我的变量中的值。例如,如果我说a = x.host_name(refresh=True),我希望从主机中检索主机名,而不是从变量_host_name 中提取。

#!/usr/local/bin/python3.7

import netaddr

"""
test.py - Used for testing

"""


class Host():
    """
    Represents a Host device

    Attributes:
        host_name: str
            Name of host
        manage_address: IP
            Managment IP address of the host
    """

    def __init__(self, host_name="", manage_address=netaddr.IPAddress("0.0.0.0")):
        """
        The constructor for the Host class.

        Parameters:
            host_name: str
                Host name of the host
            manage_address: ip
                Management IP address of this host
        """
        self._host_name = host_name
        self._manage_address = manage_address

    @property
    def host_name(self, refresh=False):
        """
        The host name configured for this host device

        Paramters:
            refresh: bool
                Should we relearn the hostname if we already know it?

        Returns: str
            Host name defined on the host
        """

        if refresh:
            # TODO: Get hostname from the host
            pass

        return self._host_name

    @host_name.setter
    def host_name(self, host_name):
        """
        Set the host name of the host

        Parameters
            host_name: str
                The host name to configure on the host

        Returns: None
        """
        self._host_name = host_name

    @property
    def manage_address(self, refresh=False):
        """
        The IP address to manage the host

        Paramters:
            refresh: bool
                Should we relearn the IP address if we already know it?

        Returns: IP
            Management IP address of the host
        """

        if refresh:
            # TODO: Get management IP address from the host
            pass

        return self._manage_address

    @manage_address.setter
    def manage_address(self, manage_address):
        """
        Set the management IP address of the host

        Parameters
            manage_address: IP
                The management IP address of the host

        Returns: None
        """
        self._manage_address = manage_address


if __name__ == '__main__':

    # A host object
    h = Host()
    h.host_name = "myhost.com"

    print(h.host_name)   # Works
    print(h.host_name(refresh=True))   # str object is not callable error
    print((str(h.manage_address(refresh=True))))   # IPAddress object is not callable error

我的猜测是我在从属性中获取值时不允许传递参数...但是如果是这种情况,我如何有条件地获取值?

【问题讨论】:

  • 您不能将参数传递给属性访问,这是没有意义的。正如错误告诉您的那样,您最终会尝试调​​用属性返回的值。如果您需要参数,请将其设为常规方法。
  • @jonrsharpe 我虽然是这样,但我不明白如何使用常规方法创建等效的 getter 和 setter。
  • ...移除装饰器并在 get_ 和 set_ 前添加?

标签: python parameters properties setter getter


【解决方案1】:

这是我想出的解决方案:

#!/usr/local/bin/python3.7

import netaddr

"""
test.py - Used for testing

"""


class Host():
    """
    Represents a Host device

    Attributes:
        host_name: str
            Name of host
        manage_address: IP
            Managment IP address of the host
    """

    def __init__(self, host_name=None, manage_address=None):
        """
        The constructor for the Host class.

        Parameters:
            host_name: str
                Host name of the host
            manage_address: ip
                Management IP address of this host
        """
        self._host_name = host_name
        self._manage_address = manage_address

    def host_name(self, refresh=False, host_name=None):
        """
        Sets and returns the host name of the host

        Parameters:
            refresh: bool
                Should we learn the value from the host even though we already know it?
            host_name: str
                Host name of the host

        Returns: str
            The host name of the host
        """
        if host_name is not None:
            self._host_name = host_name
        elif refresh or self._host_name is None:
            # TODO: Get hostname from host
            self._host_name = "thehost.com"   # Temporary

        return self._host_name

    def manage_address(self, refresh=False, manage_address=None):

        if manage_address is not None:
            self._manage_address = manage_address
        elif refresh or self._manage_address is None:
            # TODO: Get management IP address from the host
            self._manage_address = netaddr.IPAddress("127.0.0.1")   # Temporary

        return self._manage_address

if __name__ == '__main__':

    # A host object
    h = Host()
    h.host_name(host_name="myhost.com")

    print(h.host_name())   # Works
    print(h.host_name(refresh=True))
    print(h.manage_address(refresh=True))

【讨论】:

  • 我不建议使用相同的方法来获取当前值将其设置为新值。为什么h.host_name()返回一个值,而不是设置主机名默认值None
猜你喜欢
  • 2022-08-17
  • 2013-12-13
  • 2017-12-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2020-04-10
  • 2021-08-25
相关资源
最近更新 更多