Tuesday, March 5, 2019

Python Decorators

Python Decorators
  • Python Decorators
  • @accepts(int,int)
    @classmethod
    @decorator
    @property
    @returns(float)
    @staticmethod
    @funcattrs(grammar="'@' dotted_name [ '(' [arglist] ')' ]",
                   status="experimental", author="BDFL")
    
    @property
    class TempC2F:
        def __init__(self, temp = 0):
            self._t = temp
    
        def to_fahrenheit(self):
            return (self.temperature * 1.8) + 32
    
        @property
        def temperature(self):
            return self._t
    
        @temperature.setter
        def temperature(self, v):
            if v < -273:
                raise ValueError("Temperature below -273 is not possible")
            self._t = v
    

    No comments:

    Post a Comment