You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: main_book.tex
+44-7Lines changed: 44 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -852,7 +852,7 @@ \section{Decorators}
852
852
853
853
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.
854
854
\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).
856
856
857
857
\begin{lstlisting}
858
858
>>> def wrapper(func):
@@ -866,9 +866,10 @@ \section{Decorators}
866
866
After func
867
867
>>>
868
868
\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'.
870
871
\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.
872
873
\paragraph{}
873
874
Python allows us to decorate the functions using decorator, so we can write the code like this
874
875
\begin{lstlisting}
@@ -878,7 +879,7 @@ \section{Decorators}
878
879
... print("After func")
879
880
...
880
881
>>> @wrapper
881
-
... def hell_world():
882
+
... def hello_world():
882
883
... print("Hello World")
883
884
...
884
885
Before func
@@ -888,6 +889,23 @@ \section{Decorators}
888
889
\end{lstlisting}
889
890
890
891
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.
891
909
892
910
\section{Exception Handling}
893
911
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}
983
1001
\end{lstlisting}
984
1002
\subsection{Constructors}
985
1003
\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.
987
1005
988
1006
\begin{lstlisting}
989
1007
class math_op():
@@ -1052,8 +1070,28 @@ \subsection{Magic Methods}
1052
1070
\item$\textunderscore\textunderscore len \textunderscore\textunderscore()$ To Count the length of the object. (Uses with $len()$)
1053
1071
\item$\textunderscore\textunderscore dir \textunderscore\textunderscore()$ Return the methods associated with the class. (Uses with $dir()$)
1054
1072
\end{itemize}
1073
+
\section{Inheritance}
1055
1074
\section{File Operations}
1056
1075
\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
+
1057
1095
\subsection{sys module}
1058
1096
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.
1059
1097
\begin{lstlisting}
@@ -1081,11 +1119,10 @@ \subsection{os module}
1081
1119
... # os.makedirs(): To create nested directory recursively.
1082
1120
... # os.rmdir() : To remove single directory.
1083
1121
... # os.removedirs(): To remove nested directory recursively.
0 commit comments