Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

class - Python classes: method has same name as property

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.

When I call the method temperature(), I get the error

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

How do I solve this?

Code:

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...