Search code snippets, questions, articles...

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
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool
Was this helpful?
0 Comments