forked from trekhleb/learn-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_function_definition.py
More file actions
60 lines (49 loc) · 3.46 KB
/
test_function_definition.py
File metadata and controls
60 lines (49 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Function Definition
@see: https://docs.python.org/3/tutorial/controlflow.html#defining-functions
The keyword def introduces a function definition. It must be followed by the function name and the
parenthesized list of formal parameters. The statements that form the body of the function start at
the next line, and must be indented.
"""
def fibonacci_function_example(number_limit):
"""Generate a Fibonacci series up to number_limit.
The first statement of the function body can optionally be a string literal; this string
literal is the function’s documentation string, or docstring. There are tools which use
docstrings to automatically produce online or printed documentation, or to let the user
interactively browse through code; it’s good practice to include docstrings in code that you
write, so make a habit of it.
"""
# The execution of a function introduces a new symbol table used for the local variables of the
# function. More precisely, all variable assignments in a function store the value in the local
# symbol table; whereas variable references first look in the local symbol table, then in the
# local symbol tables of enclosing functions, then in the global symbol table, and finally in
# the table of built-in names. Thus, global variables cannot be directly assigned a value
# within a function (unless named in a global statement), although they may be referenced.
fibonacci_list = []
previous_number, current_number = 0, 1
while previous_number < number_limit:
# The statement result.append(a) calls a method of the list object result. A method is a
# function that ‘belongs’ to an object and is named obj.methodname, where obj is some
# object (this may be an expression), and methodname is the name of a method that is
# defined by the object’s type. Different types define different methods. Methods of
# different types may have the same name without causing ambiguity. (It is possible to
# define your own object types and methods, using classes, see Classes) The method
# append() shown in the example is defined for list objects; it adds a new element at
# the end of the list. In this example it is equivalent to result = result + [a], but
# more efficient.
fibonacci_list.append(previous_number)
# This is multiple assignment statement. We make current number to be previous one and the
# sum of previous and current to be a new current.
previous_number, current_number = current_number, previous_number + current_number
# The return statement returns with a value from a function. return without an expression
# argument returns None. Falling off the end of a function also returns None.
return fibonacci_list
def test_function_definition():
"""Function Definition"""
# Now call the function we just defined.
assert fibonacci_function_example(300) == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]
# A function definition introduces the function name in the current symbol table. The value of
# the function name has a type that is recognized by the interpreter as a user-defined function.
# This value can be assigned to another name which can then also be used as a function. This
# serves as a general renaming mechanism
fibonacci_function_clone = fibonacci_function_example
assert fibonacci_function_clone(300) == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]