python

Evaluate,check before class feald values set, bothe constructor,property option

class Product(object):

    def __init__(self, price = 0.0, name = ""):
        self.price = price
        self.name = name

    # property for __price attribute
    @property
    def price(self):
        return self.__price

    @price.setter
    def price(self, value):
        if value < 0:
            raise ValueError("Price cannot be negative")
        self.__price = value

    # property for __name attribute
    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self, value):
        for ch in value:
            if ch.isdigit():
                raise Exception("Enter valid product name")
        self.__name = value
Was this helpful?