I am looking to make my code more sophisticated and this is the issue that I am running into
class Person:
def __init__(self, age):
self.age = age
def drives(self):
if self.age >= 18:
# Do more things which driving entitles
print("you can drive")
def studies(self):
if self.age <= 25:
# Do more student stuffs
print("Good luck with your education")
bob = Person(14)
bob.drives()
bob.studies()
jim = Person(35)
jim.drives()
jim.studies()
I don't like the checks as soon as I enter the method, increasing markup. I know about decorators, are they the best thing to do here? And how would I use them for this use case? What I would like it to look like is:
class Person:
def __init__(self, age):
self.age = age
@check_if_person_age_is_greater_than_17
def drives(self):
# Do more things which driving entitles
print("you can drive")
@check_if_person_age_is_less_than_26
def studies(self):
# Do more student stuffs
print("Good luck with your education")
bob = Person(14)
bob.drives() # either this method cannot be accessed or returns nothing, since bob is 14
bob.studies() # this should work normally
jim = Person(35)
jim.drives() # this should work normally
jim.studies() # either this method cannot be accessed or returns nothing, since jim is 35
I am sorry if my question isn't concise or worthwhile.