Python 类 : method has same name as property

标签 python class

我正在构建一个类,Heating。此类的每个实例都具有“温度”属性。 Heating 还必须支持将属性“温度”打印为整数的方法 temperature()

当我调用方法 temperature() 时,出现错误

'int' object is not callable because self.temperature is already defined as an integer.

我该如何解决?

代码:

class Heating:
    """
    >>> machine1 = Heating('radiator kitchen', temperature=20)
    >>> machine2 = Heating('radiator living', minimum=15, temperature=18)
    >>> machine3 = Heating('radiator bathroom', temperature=22, minimum=18, maximum=28)
    >>> print(machine1)
    radiator kitchen: current temperature: 20.0; allowed min: 0.0; allowed max: 100.0
    >>> machine2
    Heating('radiator living', 18.0, 15.0, 100.0)
    >>> machine2.changeTemperature(8)
    >>> machine2.temperature()
    26.0
    >>> machine3.changeTemperature(-5)
    >>> machine3
    Heating('radiator bathroom', 18.0, 18.0, 28.0)
    """
    def __init__(self, name, temperature = 10, minimum = 0, maximum = 100):
        self.name = name
        self.temperature = temperature
        self.minimum = minimum
        self.maximum = maximum

    def __str__(self):
        return '{0}: current temperature: {1:.1f}; allowed min: {2:.1f}; allowed max: {3:.1f}'.format(self.name, self.temperature, self.minimum, self.maximum)

    def __repr__(self):
        return 'Heating(\'{0}\', {1:.1f}, {2:.1f}, {3:.1f})'.format(self.name, self.temperature, self.minimum, self.maximum)

    def changeTemperature(self, increment):
        self.temperature += increment

        if self.temperature < self.minimum:
            self.temperature = self.minimum

        if self.temperature > self.maximum:
            self.temperature = self.maximum

    def temperature(self):
        return self.temperature


# Test of the program
if __name__ == '__main__':
    import doctest
    doctest.testmod()

最佳答案

方法和属性不能同名。方法也是属性,尽管是您可以调用的属性。

只需将属性重命名为其他名称即可。您可以使用 _temperature:

def __init__(self, name, temperature = 10, minimum = 0, maximum = 100):
    self.name = name
    self._temperature = temperature
    self.minimum = minimum
    self.maximum = maximum

def __str__(self):
    return '{0}: current temperature: {1:.1f}; allowed min: {2:.1f}; allowed max: {3:.1f}'.format(self.name, self._temperature, self.minimum, self.maximum)

def __repr__(self):
    return 'Heating(\'{0}\', {1:.1f}, {2:.1f}, {3:.1f})'.format(self.name, self._temperature, self.minimum, self.maximum)

def changeTemperature(self, increment):
    self._temperature += increment

    if self.temperature < self.minimum:
        self._temperature = self.minimum

    if self.temperature > self.maximum:
        self._temperature = self.maximum

def temperature(self):
    return self._temperature

现在你的类有 _temperature(整数)和 temperature()(方法);后者返回前者。

关于Python 类 : method has same name as property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39210250/

相关文章:

c# - 什么时候适合使用 C# 分部类?

c++ - 类函数指针作为静态函数中的参数

python - 如何在 DRF 中使用基于异步函数的 View ?

python - 如何识别数量不断增加且文件名形式相似的文件?

java - 将不同类的面板添加到框架

c# - 将匿名类数组传递给方法并循环 C#

html - css 类不影响

python - Windows 10 上的 Tensorflow 安装问题

python - 我应该如何处理 twisted.application.internet.ClientService 中的重新连接?

python 2.7 : Thread local storage's instantiation when first accessed?