property setter in __init__ method?

Viewed 3456

I have this hypothetical situation where I need to check if a Phone instance is being created with price less than 100 and, if it is, then I have to warn the user that price cannot be less than 100.

I am using the below for this example:

class Phone(object):
    def __init__(self,v):
        self._cost = v

    @property
    def cost(self):
        return self._cost

    @cost.setter
    def cost(self,value):
        if value < 100:
            print("Cost cannot be less than 100")
        else:
             self._cost = value

s8 = Phone(98)

But this code is not letting the user know that a phone cannot be created with price less than 100. What is the use of having a setter function for a property in Python if we cannot check the values when we are initializing the object? Am I doing anything wrong here?

1 Answers
Related