Today we are going to share a Python program to illustrate closures. If you are a python beginner and want to start learning the python programming, then keep your close attention in this tutorial as I am going to share a Python program to illustrate closures.
To increase your Python knowledge, practice all Python programs, here is a collection of 100+ Python problems with solutions.
Copy the below python program and execute it with the help of python compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import logging logging.basicConfig(filename='example.log', level=logging.INFO) def logger(func): def log_func(*args): logging.info( 'Running "{}" with arguments {}'.format(func.__name__, args)) print(func(*args)) # Necessary for closure to work (returning WITHOUT parenthesis) return log_func def add(x, y): return x+y def sub(x, y): return x-y add_logger = logger(add) sub_logger = logger(sub) add_logger(3, 3) add_logger(4, 5) sub_logger(10, 5) sub_logger(20, 10) |
6
9
5
10
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.