Skip to content

Commit b510048

Browse files
committed
Added more descriptive explaination of decorators.
Signed-off-by: Amol Kahat <amolkahat@gmail.com>
1 parent 48ddf90 commit b510048

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

main_book.tex

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ \section{Decorators}
852852
853853
Yes.. it will show you address of that function. It is telling you that function is present at some location on memory. If you will get address of that function then you can play around it.
854854
\paragraph{}
855-
Remember we didn't called the function but we just print it's memory location. If that is the case then I can pass this function as an argument to another function.
855+
Remember we did not called the function yet, we just print it's memory location. If that is the case then we can pass this function as an argument to another function (like call by reference in C).
856856
857857
\begin{lstlisting}
858858
>>> def wrapper(func):
@@ -866,9 +866,10 @@ \section{Decorators}
866866
After func
867867
>>>
868868
\end{lstlisting}
869-
In above example, I wrote another function name as wrapper and passed one argument to it, and called it with two parameters. When I called wrapper function that time I passed add function reference to it.
869+
In above example, I wrote another function name as 'wrapper' and passed one argument 'add' in it, and inside the wrapper function I called function 'func' with two parameters.
870+
After hitting enter, we could see that function add print addition 30 in between 'Before func' and 'After func'.
870871
\paragraph{}
871-
So basically this happened, I passed add to wrapper and when wrapper get called it printed "Before func" then it will call add function which takes two arguments and return the result, After that it will print "After func". So this passed add function is get called within the wrapper function.
872+
So basically this happened, I passed 'add' to 'wrapper' and when wrapper get called it printed "Before func" then it will call 'add' function which takes two arguments and return and print the result, After that it will print "After func". So this passed add function is get called within the 'wrapper' function.
872873
\paragraph{}
873874
Python allows us to decorate the functions using decorator, so we can write the code like this
874875
\begin{lstlisting}
@@ -878,7 +879,7 @@ \section{Decorators}
878879
... print("After func")
879880
...
880881
>>> @wrapper
881-
... def hell_world():
882+
... def hello_world():
882883
... print("Hello World")
883884
...
884885
Before func
@@ -888,6 +889,23 @@ \section{Decorators}
888889
\end{lstlisting}
889890

890891
When you decorate any function, by default python interpreter call wrapper function add pass "hello\textunderscore world" function reference in to it.
892+
\paragraph{}
893+
Basically we can write above decorator function in simple form
894+
\begin{lstlisting}
895+
>>> def wrapper(func):
896+
... print("Before func")
897+
... func()
898+
... print("After func")
899+
...
900+
>>> @wrapper
901+
... def hello_world():
902+
... print("Hello World")
903+
...
904+
Before func
905+
Hello World
906+
After func
907+
\end{lstlisting}
908+
Okay.. now I expect you got some understanding of the decorators. You might have notice that it get called automatically by the interpreter.
891909

892910
\section{Exception Handling}
893911
In Python there are multiple built in exceptions are present. Exceptions are designed to handle the error in running program. Consider if you are converting the strings to the integer, which is against the Python programming language rule. To handle this situation exceptions are used. If exceptions are not used then program will crash.
@@ -983,7 +1001,7 @@ \subsection{Objects}
9831001
\end{lstlisting}
9841002
\subsection{Constructors}
9851003
\subsubsection{Default Constructors}
986-
Constructors are the methods which are called when the objects are created. In Python you can defined the constructors, which will get called when object is get created.
1004+
Constructors are the methods which are called when the objects are created. In Python you can defined the constructors, which get called when object got created.
9871005

9881006
\begin{lstlisting}
9891007
class math_op():
@@ -1052,8 +1070,28 @@ \subsection{Magic Methods}
10521070
\item $\textunderscore\textunderscore len \textunderscore\textunderscore()$ To Count the length of the object. (Uses with $len()$)
10531071
\item $\textunderscore\textunderscore dir \textunderscore\textunderscore()$ Return the methods associated with the class. (Uses with $dir()$)
10541072
\end{itemize}
1073+
\section{Inheritance}
10551074
\section{File Operations}
10561075
\section{Modules}
1076+
Implementing modules in Python is very easy. Modules are nothing but the file with the class. If you want to use those classes then you can directly import those classes in your code.
1077+
Let's create on file wich has 2 classes. Class A has get_a and set_a methods and Class B has get_b and set_b methods.
1078+
1079+
\begin{lstlisting}
1080+
class A:
1081+
a = 10
1082+
def get_a(self):
1083+
return self.a
1084+
1085+
def set_a(self, a):
1086+
self.a = a
1087+
1088+
class B:
1089+
b = 20
1090+
def get_b(self):
1091+
return self.b
1092+
1093+
\end{lstlisting}
1094+
10571095
\subsection{sys module}
10581096
sys module is used to check the which type of system it is, This module gives you the details about the system, consider you don't know which OS you have Mac OS, Linux OS and Windows, you can find out it using sys module.
10591097
\begin{lstlisting}
@@ -1081,11 +1119,10 @@ \subsection{os module}
10811119
... # os.makedirs(): To create nested directory recursively.
10821120
... # os.rmdir() : To remove single directory.
10831121
... # os.removedirs(): To remove nested directory recursively.
1084-
10851122
\end{lstlisting}
10861123
\subsection{re module}
10871124
\subsection{sqlite module}
10881125
\section{Python Built In methods}
10891126
\part{Testing with Python}
10901127
\part{Django}
1091-
\end{document}
1128+
\end{document}

0 commit comments

Comments
 (0)