Object-Oriented Programming/Properties/Python3

From Wikiversity
Jump to navigation Jump to search

main.py[edit | edit source]

"""This program demonstrates use of the Temperature class."""

from temperature import Temperature

temperature = Temperature(celsius=37)
print(temperature.fahrenheit)

temperature = Temperature(fahrenheit=98.6)
print(temperature.celsius)

temperature = Temperature()
temperature.celsius = 37
print(temperature.fahrenheit)

temperature = Temperature()
temperature.fahrenheit = 98.6
print(temperature.celsius)

temperature = Temperature()
print(temperature.to_celsius(98.6))
print(temperature.to_fahrenheit(37))

temperature.py[edit | edit source]

"""Temperature container / converter.

Examples:
    from temperature import Temperature
    
    temperature = Temperature(celsius=37)
    print(temperature.fahrenheit)
    
    temperature = Temperature(fahrenheit=98.6)
    print(temperature.celsius)
    
    temperature = Temperature()
    temperature.celsius = 37
    print(temperature.fahrenheit)
    
    temperature = Temperature()
    temperature.fahrenheit = 98.6
    print(temperature.celsius)
    
    temperature = Temperature()
    print(temperature.to_celsius(98.6))
    print(temperature.to_fahrenheit(37))

"""


class Temperature(object):
    """Supports Fahrenheit and Celius temperatures."""
    
    _celsius = None
    _fahrenheit = None


    @property
    def celsius(self):
        """Gets Celsius temperature.

        Returns:
            float: Celsius temperature

        """
        return self._celsius
    
        
    @celsius.setter
    def celsius(self, value):
        """Sets Celsius temperature.

        Args:
            value (float): Celsius temperature
    
        """
        self._celsius = float(value)
        self._fahrenheit = self.to_fahrenheit(self.celsius)
        
    
    @property
    def fahrenheit(self):
        """Gets Fahrenheit temperature.

        Returns:
            float: Fahrenheit temperature

        """
        return self._fahrenheit
        
    
    @fahrenheit.setter
    def fahrenheit(self, value):
        """Sets Fahrenheit temperature.

        Args:
            value (float): Fahrenheit temperature
    
        """
        self._fahrenheit = float(value)
        self._celsius = self.to_celsius(self.fahrenheit)
        
    
    def __init__(self, celsius=None, fahrenheit=None):
        """Creates a temperature object.
        
        Args:
            celius (float):     Optional Celius temperature.
            fahrenheit (float): Optional Fahrenheit temperature.

        """
        
        if celsius != None:
            self.celsius = float(celsius)
    
        if fahrenheit != None:
            self.fahrenheit = float(fahrenheit)
    
    
    def to_celsius(self, fahrenheit):
        """Converts Fahrenheit temperature to Celsius.
    
        Args:
            fahrenheit (float): Fahrenheit temperature to be converted
    
        Returns:
            float: Celsius temperature
    
        """
        return (fahrenheit - 32) * 5 / 9
        
    
    def to_fahrenheit(self, celsius):
        """Converts Celsius temperature to Fahrenheit.
    
        Args:
            celsius (float): Celsius temperature to be converted
    
        Returns:
            float: Fahrenheit temperature
    
        """
        return celsius * 9 / 5 + 32

Try It[edit | edit source]

Copy and paste the code above into one of the following free online development environments or use your own Python3 compiler / interpreter / IDE.

See Also[edit | edit source]