0
class Temperature:
    def __init__(self, temperature):
        """Gets temperature from user"""    
        self.__temperature = temperature

    def aboveFreezing(self):
        """Returns True if temperature above freezing point"""
        raise NotImplementedError("Method aboveFreezing not implemented")

    def convertToFahren(self):
        """Returns a new Temperature object converted to degrees Fahrenheit"""
        raise NotImplementedError("Method convertToFahren not implemented")

    def convertToCelsius(self):
        """Returns a new Temperature object converted to degrees Celsius"""
        raise NotImplementedError("Method convertToCelsius not implemented")

    def convertToKelvin(self):
        """Returns a new Temperature object converted to degrees Kelvin"""
        raise NotImplementedError("Method convertToKelvin not implemented")

class Kelvin(Temperature):

    def __init__(self, temperature):
        Temperature.__init__(self,temperature)

    def __str__(self):
        """Returns string of temperature"""
        return self.__temperature + 'degrees Kelvin.'

    def aboveFreezing(self):
        """Returns True if temperature above freezing point"""
        if self.__temperature > 273.15:
            return True
        else:
            return False 

    def convertToFahren(self):
        """Returns a copy Kevin to Fahren"""
        return ((self.__temperature * 9/5) - 459.67)

    def convertToCelsius(self):
        """Returns conversion from Kelvin to Celsius"""
        return self.__temperature - 273.15

    def convertToKelvin(self):
        """Returns copy of Kelvin"""
        return self.__temperature 

I try to run the Kelvin class so I try:

t1 = Kelvin(33)
print(t1)

But it tells me that:

    return self.__temperature + 'degrees Kelvin.'
AttributeError: 'Kelvin' object has no attribute '_Kelvin__temperature'

I'm new to classes. I think it is because I have an error in:

def __init__(self, temperature):
    Temperature.__init__(self,temperature)

So if this is the problem, how do I get the Kelvin class to have the value of temperature? I have to make Kelvin a subclass of Temperature. If I didn't have to do that I know I could do: self.__temperature = temperature

But how I assign the temperature from the Temperature class to the subclass Kelvin?

Paweł Kordowski
  • 2,688
  • 1
  • 14
  • 21
Trey
  • 63
  • 1
  • 9
  • Briefly: the whole point of prefixing a variable with `__` is to mangle the name. If you want it to be easily accessible from other areas, don't mangle it. Use a single underscore, or even none at all. – TigerhawkT3 Nov 22 '15 at 00:00
  • Well, my book requires us to use the double underscore for the "init". So how would I bypass that? My book gives an example of creating a subclass and I formatted it similar to theirs. So I am confused on why it isn't working. – Trey Nov 22 '15 at 00:05
  • There is a difference between `__var` and `__var__`. Please thoroughly read the linked duplicate question and its answers. – TigerhawkT3 Nov 22 '15 at 00:06

0 Answers0