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: docs/functions.rst
+49-16Lines changed: 49 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,10 @@
3
3
Functions
4
4
=========
5
5
6
-
Reusing the same code is required many times within a same program. Functions help us to do so. We write the things we have to do repeatedly in a function then call it where ever required. We already saw build in functions like *len()*, *divmod()*.
6
+
Reusing the same code is required many times within a same program. Functions
7
+
help us to do so. We write the things we have to do repeatedly in a function
8
+
then call it where ever required. We already saw build in functions like
9
+
*len()*, *divmod()*.
7
10
8
11
Defining a function
9
12
===================
@@ -16,22 +19,26 @@ We use *def* keyword to define a function. General syntax is like
16
19
statement1
17
20
statement2
18
21
19
-
Let us write a function which will take two integers as input and then return the sum.
22
+
Let us write a function which will take two integers as input and then return
23
+
the sum.
20
24
21
25
::
22
26
23
27
>>> def sum(a, b):
24
28
... return a + b
25
29
26
-
In the second line with the *return* keyword, we are sending back the value of *a + b* to the caller. You must call it like
30
+
In the second line with the *return* keyword, we are sending back the value of
31
+
*a + b* to the caller. You must call it like
27
32
28
33
::
29
34
30
35
>>> res = sum(234234, 34453546464)
31
36
>>> res
32
37
34453780698L
33
38
34
-
Remember the palindrome program we wrote in the last chapter. Let us write a function which will check if a given string is palindrome or not, then return *True* or *False*.
39
+
Remember the palindrome program we wrote in the last chapter. Let us write a
40
+
function which will check if a given string is palindrome or not, then return
41
+
*True* or *False*.
35
42
36
43
::
37
44
@@ -72,7 +79,13 @@ The output
72
79
inside change function 90
73
80
After the function call 9
74
81
75
-
First we are assigning *9* to *a*, then calling change function, inside of that we are assigning *90* to *a* and printing *a*. After the function call we are again printing the value of *a*. When we are writing *a = 90* inside the function, it is actually creating a new variable called *a*, which is only available inside the function and will be destroyed after the function finished. So though the name is same for the variable *a* but they are different in and out side of the function.
82
+
First we are assigning *9* to *a*, then calling change function, inside of that
83
+
we are assigning *90* to *a* and printing *a*. After the function call we are
84
+
again printing the value of *a*. When we are writing *a = 90* inside the
85
+
function, it is actually creating a new variable called *a*, which is only
86
+
available inside the function and will be destroyed after the function finished.
87
+
So though the name is same for the variable *a* but they are different in and
88
+
out side of the function.
76
89
77
90
::
78
91
@@ -87,7 +100,9 @@ First we are assigning *9* to *a*, then calling change function, inside of that
87
100
change(a)
88
101
print("After the function call ", a)
89
102
90
-
Here by using global keyword we are telling that *a* is globally defined, so when we are changing a's value inside the function it is actually changing for the *a* outside of the function also.
103
+
Here by using global keyword we are telling that *a* is globally defined, so
104
+
when we are changing a's value inside the function it is actually changing for
105
+
the *a* outside of the function also.
91
106
92
107
The output
93
108
::
@@ -100,7 +115,8 @@ The output
100
115
Default argument value
101
116
======================
102
117
103
-
In a function variables may have default argument values, that means if we don't give any value for that particular variable it will be assigned automatically.
118
+
In a function variables may have default argument values, that means if we don't
119
+
give any value for that particular variable it will be assigned automatically.
104
120
105
121
::
106
122
@@ -110,7 +126,9 @@ In a function variables may have default argument values, that means if we don't
110
126
... else:
111
127
... return False
112
128
113
-
In the above example we have written *b = -99* in the function parameter list. That means if no value for *b* is given then b's value is *-99*. This is a very simple example of default arguments. You can test the code by
129
+
In the above example we have written *b = -99* in the function parameter list.
130
+
That means if no value for *b* is given then b's value is *-99*. This is a very
131
+
simple example of default arguments. You can test the code by
114
132
115
133
::
116
134
@@ -123,7 +141,8 @@ In the above example we have written *b = -99* in the function parameter list. T
123
141
124
142
Remember that you can not have an argument without default argument if you already have one argument with default values before it. Like *f(a, b=90, c)* is illegal as *b* is having a default value but after that *c* is not having any default value.
125
143
126
-
Also remember that default value is evaluated only once, so if you have any mutable object like list it will make a difference. See the next example
144
+
Also remember that default value is evaluated only once, so if you have any
145
+
mutable object like list it will make a difference. See the next example
127
146
128
147
::
129
148
@@ -153,7 +172,7 @@ To avoid this you can write more idiomatic Python, like the following
153
172
>>> print(f(2))
154
173
[2]
155
174
156
-
.. note:: To understand more read `this link <http://docs.python.org/2/tutorial/controlflow.html#default-argument-values>`_.
175
+
.. note:: To understand more read `this url <https://docs.python.org/3/tutorial/controlflow.html#default-argument-values>`_.
157
176
158
177
Keyword arguments
159
178
=================
@@ -169,7 +188,10 @@ Keyword arguments
169
188
>>> func(b=12, c = 24, a = -1)
170
189
a is -1 and b is 12 and c is 24
171
190
172
-
In the above example you can see we are calling the function with variable names, like *func(12, c = 24)*, by that we are assigning *24* to *c* and *b* is getting its default value. Also remember that you can not have without keyword based argument after a keyword based argument. like
191
+
In the above example you can see we are calling the function with variable
192
+
names, like *func(12, c = 24)*, by that we are assigning *24* to *c* and *b* is
193
+
getting its default value. Also remember that you can not have without keyword
194
+
based argument after a keyword based argument. like
173
195
174
196
::
175
197
@@ -182,8 +204,9 @@ In the above example you can see we are calling the function with variable names
182
204
Keyword only argument
183
205
=====================
184
206
185
-
We can also mark the arguments of function as keyword only. That way while calling the function, the user will be forced to use
186
-
correct keyword for each parameter.
207
+
We can also mark the arguments of function as keyword only. That way while
208
+
calling the function, the user will be forced to use correct keyword for each
209
+
parameter.
187
210
188
211
::
189
212
@@ -204,7 +227,9 @@ correct keyword for each parameter.
204
227
Docstrings
205
228
==========
206
229
207
-
In Python we use docstrings to explain how to use the code, it will be useful in interactive mode and to create auto-documentation. Below we see an example of the docstring for a function called *longest_side*.
230
+
In Python we use docstrings to explain how to use the code, it will be useful in
231
+
interactive mode and to create auto-documentation. Below we see an example of
232
+
the docstring for a function called *longest_side*.
208
233
209
234
210
235
::
@@ -254,8 +279,9 @@ In Python any function can act as higher order function.
254
279
map function
255
280
=============
256
281
257
-
`map` is a very useful higher order function in Python. It takes one function and an iterator
258
-
as input and then applies the function on each value of the iterator and returns a list of results.
282
+
`map` is a very useful higher order function in Python. It takes one function
283
+
and an iterator as input and then applies the function on each value of the
284
+
iterator and returns a list of results.
259
285
260
286
Example::
261
287
@@ -267,3 +293,10 @@ Example::
267
293
>>> print(list(map(square, lst)))
268
294
[1, 4, 9, 16, 25]
269
295
296
+
297
+
HOWTO Write a function
298
+
========================
299
+
300
+
Watch `this talk <https://www.youtube.com/watch?v=rrBJVMyD-Gs>`_ by Jack
301
+
Diederich at PyCon US 2018 to learn more about how to write clean Python
0 commit comments