Python类:方法与属性同名 [英] Python classes: method has same name as property

查看:256
本文介绍了Python类:方法与属性同名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一类供暖系统。此类的每个实例均具有温度属性。强制性的是,Heating还必须支持将temperature属性打印为整数的temperature()方法。

I'm constructing a class Heating. Every instance of this class has the property 'temperature'. It's mandatory that Heating also supports the method temperature() that prints the property 'temperature' as an integer.

当我调用方法temperature()时,会收到错误消息不能调用int对象,因为self.temperature已经定义为整数。

When I call the method temperature() I get the error '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




# testen van het programma 
if __name__ == '__main__':
    import doctest
    doctest.testmod()


推荐答案

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

You can't have both a method and an attribute with the same name. Methods are attributes too, albeit ones that you can call.

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

Just rename the attribute to something else. You could use _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()(该方法);后者返回前者。

Now your class has both _temperature (the integer) and temperature() (the method); the latter returns the former.

这篇关于Python类:方法与属性同名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆