FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
learn-python/src/functions/test_function_definition.py at master · Gudzikgit/learn-python · GitHub
Gudzikgit
/
learn-python
Public
forked from
trekhleb/learn-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
learn-python
/
src
/
functions
/
test_function_definition.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (49 loc) · 3.46 KB
Breadcrumbs
learn-python
/
src
/
functions
/
test_function_definition.py
Copy path
File metadata and controls
60 lines (49 loc) · 3.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
]
Back
|
FazBrowse Home
|
New Git URL