Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Python property Class

# using property class
class Celsius:
    def __init__(self, temperature=0):
        self.temperature = temperature
    def to_fahrenheit(self):
        return (self.temperature * 1.8) + 32
    # getter
    def get_temperature(self):
        print("Getting value...")
        return self._temperature
    # setter
    def set_temperature(self, value):
        print("Setting value...")
        if value < -273.15:
            raise ValueError("Temperature below -273.15 is not possible")
        self._temperature = value
    # creating a property object
    temperature = property(get_temperature, set_temperature)
Source by www.programiz.com #
 
PREVIOUS NEXT
Tagged: #Python #property #Class
ADD COMMENT
Topic
Name
8+2 =