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?
Similar Posts
- Python program to convert a List to Set
- Python check NaN values with and without using packages
- Check for falsy values - None, False, blank in Python
- Check if a column contains zero values only in Pandas DataFrame
- [Python] Namedtuples can be a great alternative to defining a class manually
- [Python] Create a list of objects using class names
- [Python] Function returns multiple values