I have this code:
class Human(object):
def __init__(self, amount):
self._age = self.age = amount
@property
def age(self):
return self._age
@age.setter
def age(self, amount):
if amount > 50:
self._age = amount
else:
print('You are too young')
What I am wondering about is, is that a viable way to make the argument passed to __init__
pass through some sort of logic implemented in the property ? I browsed through a lot of answers here and read the documentation, and still I couldn't find any mention about such a thing. The code is working as intended when creating a test instance of the class:
bob = Human(30)
yields the proper warning. Attempting to set the name property afterwards does too, and when given a proper argument (in this case any number > 50) everything seems to work as intended. Is there some sort of caveat about this and if yes, what is the most Pythonic way to approach this problem?