Skip to content

Commit 1246f2b

Browse files
committed
Adds talk from Jack Diederich
1 parent 9b168f3 commit 1246f2b

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

docs/functions.rst

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
Functions
44
=========
55

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()*.
710

811
Defining a function
912
===================
@@ -16,22 +19,26 @@ We use *def* keyword to define a function. General syntax is like
1619
statement1
1720
statement2
1821

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.
2024

2125
::
2226

2327
>>> def sum(a, b):
2428
... return a + b
2529

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
2732

2833
::
2934

3035
>>> res = sum(234234, 34453546464)
3136
>>> res
3237
34453780698L
3338

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*.
3542

3643
::
3744

@@ -72,7 +79,13 @@ The output
7279
inside change function 90
7380
After the function call 9
7481

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.
7689

7790
::
7891

@@ -87,7 +100,9 @@ First we are assigning *9* to *a*, then calling change function, inside of that
87100
change(a)
88101
print("After the function call ", a)
89102

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.
91106

92107
The output
93108
::
@@ -100,7 +115,8 @@ The output
100115
Default argument value
101116
======================
102117

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.
104120

105121
::
106122

@@ -110,7 +126,9 @@ In a function variables may have default argument values, that means if we don't
110126
... else:
111127
... return False
112128

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
114132

115133
::
116134

@@ -123,7 +141,8 @@ In the above example we have written *b = -99* in the function parameter list. T
123141

124142
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.
125143

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
127146

128147
::
129148

@@ -153,7 +172,7 @@ To avoid this you can write more idiomatic Python, like the following
153172
>>> print(f(2))
154173
[2]
155174

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>`_.
157176

158177
Keyword arguments
159178
=================
@@ -169,7 +188,10 @@ Keyword arguments
169188
>>> func(b=12, c = 24, a = -1)
170189
a is -1 and b is 12 and c is 24
171190

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
173195

174196
::
175197

@@ -182,8 +204,9 @@ In the above example you can see we are calling the function with variable names
182204
Keyword only argument
183205
=====================
184206

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.
187210

188211
::
189212

@@ -204,7 +227,9 @@ correct keyword for each parameter.
204227
Docstrings
205228
==========
206229

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*.
208233

209234

210235
::
@@ -254,8 +279,9 @@ In Python any function can act as higher order function.
254279
map function
255280
=============
256281

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.
259285

260286
Example::
261287

@@ -267,3 +293,10 @@ Example::
267293
>>> print(list(map(square, lst)))
268294
[1, 4, 9, 16, 25]
269295

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
302+
functions and many other tips.

0 commit comments

Comments
 (0)