Question: I have been trying to find a use case to learn decorators and I think I have found one which is relevant to me. I am using the following codes. In the file class1.py I have: import pandas as pd, os class myClass(): def __init__(self): fnDone = f'C:\user1\Desktop\loc1\fn.csv' if os.path.exists(fnDone): return self.Fn1() pd.DataFrame({'Done': 1}, index=[0]).to_csv(fnDone) def Fn1(self): print('something') if __name__ == '__main__': myClass()  In the file class2.py I have: class myClassInAnotherFile(): def __init__(self): fnDone = f'C:\user1\Desktop\loc2\fn.csv' if os.path.exists(fnDone): return self.Fn1() self.Fn2() pd.DataFrame({'Done': 1}, index=[0]).to_csv(fnDone) def Fn1(self): print('something') def Fn2(self): print('something else') if __name__ == '__main__': myClassInAnotherFile('DoneFile12)  Is there a way to define a generic decorator code in another file called utilities.py so that I can do something of the following sort: Desired in the file class1.py I have: import pandas as pd, os class myClass(): def __init__(self): fnDone = f'C:\user1\Desktop\loc1\fn.csv' self.Fn1() pd.DataFrame({'Done': 1}, index=[0]).to_csv(fnDone) def Fn1(self): print('something') if __name__ == '__main__': @myDecorator myClass()  In the file class2.py I have: class myClassInAnotherFile(): def __init__(self): fnDone = f'C:\user1\Desktop\loc2\fn.csv' self.Fn1() self.Fn2() pd.DataFrame({'Done': 1}, index=[0]).to_csv(fnDone) def Fn1(self): print('something') def Fn2(self): print('something else') if __name__ == '__main__': @myDecorator myClassInAnotherFile()  Essentially mimicking the original behavior using a decorator. Edit1: I am looking to extend the functionality of my class definitions. In the both original class definitions, I repeat the code which checks for fnDone file and if it is present, exits the class. Goal is to have a decorator which checks for the fnDone file and exits the class if it is present. Edit2: I can do this as a function also but I am trying to learn how to extend functionality of a class or method using decorators. Edit3: Does it make it easier if I have the following instead in class1.py: def myClass(): fnDone = f'C:\user1\Desktop\loc1\fn.csv' if os.path.exists(fnDone): return self.Fn1() pd.DataFrame({'Done': 1}, index=[0]).to_csv(fnDone) def Fn1(self): print('something') if __name__ == '__main__': myClass()  and class2.py as following: def myClassInAnotherFile(): fnDone = f'C:\user1\Desktop\loc2\fn.csv' if os.path.exists(fnDone): return self.Fn1() self.Fn2() pd.DataFrame({'Done': 1}, index=[0]).to_csv(fnDone) def Fn1(self): print('something') def Fn2(self): print('something else') if __name__ == '__main__': myClassInAnotherFile('DoneFile12) 

Answer: Because fnDone is a local variable rather than a parameter, it makes using a decorator a bit awkward. If you modify the code slightly to pass in fnDone as a parameter, it makes using a decorator more of a viable option. For example, you could make a decorator that wraps the constructor of an object, and checks if the file passed in exist or not: import os.path from functools import wraps import pandas as pd def check_file_exists(f): @wraps(f) def _inner(self, fn_done): if os.path.exists(fn_done): return f(self, fn_done) return _inner class MyClass: @check_file_exists def __init__(self, fn_done) -> None: pd.DataFrame({'Done': 1}, index=[0]).to_csv(fn_done) if __name__ == "__main__": MyClass("fn.csv")