“Python Property” Ответ

Python Property

class Car():
    def __init__(self):
        self._seat = None

    @property # getter
    def seat(self):
        return self._seat

    @seat.setter # setter
    def seat(self, seat):
        self._seat = seat


c = Car()
c.seat = 6
print(c.seat)
Wojak's distant cousin

@property Python

# 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)


human = Celsius(37)

print(human.temperature)

print(human.to_fahrenheit())

human.temperature = -300
Drab Dog

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)
SAMER SAEID

Ответы похожие на “Python Property”

Вопросы похожие на “Python Property”

Больше похожих ответов на “Python Property” по Python

Смотреть популярные ответы по языку

Смотреть другие языки программирования