Please have a look at the below code
class MyStudent:
def __init__(self, student_id, student_name):
self._student_id = student_id
self._student_name = student_name
def get_student_id(self):
return self._student_id
def get_student_name(self):
return self._student_name
student1 = MyStudent(student_id=123, student_name="ABC")
student1.get_student_id()
student1.get_student_name()
I would like to run some code like adding the student to the DB whenever student1.get_student_id()
or student1.get_student_name()
is invoked (or when get() is accessed. Please correct me if i used the wrong descriptor). And I have to do this via Decorators only for multiple classes like below
@save_student_to_db
class MyStudent:......
How can this be achieved using Decorators? I need to use a single decorator for multiple classes which can have any method. Whenever any method (apart for the ones starting with _ or __) of any class is called the decorator should save the data to DB