11Decorators
22----------
33
4- Decorators are a significant part of Python. In simple words they are
4+ Decorators are a significant part of Python. In simple words: they are
55functions which modify the functionality of another function. They help
66to make our code shorter and more Pythonic. Most of the beginners do not
77know where to use them so I am going to share some areas where
8- decorators can make your code concise.
8+ decorators can make your code more concise.
99
1010First, let's discuss how to write your own decorator.
1111
@@ -44,7 +44,7 @@ First of all let's understand functions in python:
4444 Defining functions within functions:
4545^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4646
47- So those are the basics when it comes to functions. Lets take your
47+ So those are the basics when it comes to functions. Let's take your
4848knowledge one step further. In Python we can define functions inside
4949other functions:
5050
@@ -77,8 +77,8 @@ other functions:
7777 # outputs: NameError: name 'greet' is not defined
7878
7979 So now we know that we can define functions in other functions. In
80- simpler words we can make nested functions. Now you need to learn one
81- more thing that functions can return functions too.
80+ other words: we can make nested functions. Now you need to learn one
81+ more thing, that functions can return functions too.
8282
8383Returning functions from within functions:
8484^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -112,13 +112,13 @@ can return it as an output as well:
112112
113113 Just take a look at the code again. In the ``if/else `` clause we are
114114returning ``greet `` and ``welcome ``, not ``greet() `` and ``welcome() ``.
115- Why is that? It is so because when you put parentheses around it the
116- function gets executed whereas if you don't put parenthesis around it
115+ Why is that? It's because when you put a pair of parentheses after it, the
116+ function gets executed; whereas if you don't put parenthesis after it,
117117then it can be passed around and can be assigned to other variables
118- without executing it. Did you get it ? Let me explain it a little bit in
118+ without executing it. Did you get it? Let me explain it in a little bit
119119more detail. When we write ``a = hi() ``, ``hi() `` gets executed and
120- because the name is yasoob by default, the function greet is returned.
121- If we change the statement to ``a = hi(name = "ali") `` then the welcome
120+ because the name is yasoob by default, the function `` greet `` is returned.
121+ If we change the statement to ``a = hi(name = "ali") `` then the `` welcome ``
122122function will be returned. We can also do print ``hi()() `` which outputs
123123*now you are in the greet() function *.
124124
@@ -144,7 +144,7 @@ are. Decorators let you execute code before and after a function.
144144Writing your first decorator:
145145^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
146146
147- In the last example we actually made a decorator! Lets modify the
147+ In the last example we actually made a decorator! Let's modify the
148148previous decorator and make a little bit more usable program:
149149
150150.. code :: python
0 commit comments