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 = name

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

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

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

    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?