Posts

Showing posts from July, 2024

Decorators in python - Part 1

Decorators in python is very useful in data hiding and performing pre and post operation using simple function object. Below is a example of decorator and how its working in python interpretor. def sample_decorator ( func ):     def wrapper ( val ):         print ( "calling a function for pre-processing" )         return_value = func ( val )         print ( "calling post-processing function" )         return return_value     return wrapper def sample_function ( val ):     print ( f "hai from sample function { val } " ) if __name__ == "__main__" :     sample_function = sample_decorator ( sample_function )     sample_function ( 10 ) In python everything is object so what we are doing above is adding a syntatical sugar using a decorator that replaces the sample_function object with newly created sample_function object. We have created sample_function ...