Python classes: method has same name as property
<p>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.</p>

<p>When I call the method temperature() I get the error 'int' object is not callable because self.temperature is already defined as an integer.</p>

<p>How do I solve this?</p>

<p>code:</p>

<pre><code>class Heating:
    """
    &gt;&gt;&gt; machine1 = Heating('radiator kitchen', temperature=20)
    &gt;&gt;&gt; machine2 = Heating('radiator living', minimum=15, temperature=18)    
    &gt;&gt;&gt; machine3 = Heating('radiator bathroom', temperature=22, minimum=18, maximum=28)
    &gt;&gt;&gt; print(machine1)
    radiator kitchen: current temperature: 20.0; allowed min: 0.0; allowed max: 100.0
    &gt;&gt;&gt; machine2
    Heating('radiator living', 18.0, 15.0, 100.0)
    &gt;&gt;&gt; machine2.changeTemperature(8)
    &gt;&gt;&gt; machine2.temperature()
    26.0
    &gt;&gt;&gt; machine3.changeTemperature(-5)
    &gt;&gt;&gt; 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 &lt; self.minimum:
            self.temperature = self.minimum

        if self.temperature &gt; self.maximum:
            self.temperature = self.maximum

    def temperature(self):
        return self.temperature




# testen van het programma 
if __name__ == '__main__':
    import doctest
    doctest.testmod()
</code></pre>

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

<p>Just rename the attribute to something else. You could use <code>_temperature</code>:</p>

<pre><code>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 &lt; self.minimum:
        self._temperature = self.minimum

    if self.temperature &gt; self.maximum:
        self._temperature = self.maximum

def temperature(self):
    return self._temperature
</code></pre>

<p>Now your class has both <code>_temperature</code> (the integer) and <code>temperature()</code> (the method); the latter returns the former.</p>

python class