Functions¶
def my_fun1(i):
print("in the function, here's i:" , i)
return
my_fun1(10)
my_fun1(5)
in the function, here's i: 10 in the function, here's i: 5
Arguments¶
Note: Python passes mutable objects by reference, similar to assignment.
def my_fun2(i, A, B):
i = 5
A[0] = 5
B = {4, 5, 6}
i = 1
A = [1, 2, 3]
B = {1, 2, 3}
my_fun2(i, A, B)
print('i={}; A={}; B={}'.format(i, A, B))
i=1; A=[5, 2, 3]; B={1, 2, 3}
This is not surprising, because passing arguments is essentially the same as assigning variables in Python.
i_g = 1
A_g = [1, 2, 3]
B_g = {1, 2, 3}
my_fun2(i=i_g, A=A_g, B=B_g)
print('i_g={}; A_g={}; B_g={}'.format(i_g, A_g, B_g))
i_g=1; A_g=[5, 2, 3]; B_g={1, 2, 3}
Arguments can have optional arguments, which have default values.
def check_answer(val, correct_answer="a", third_arg=1):
if val == correct_answer:
return True
else:
return False
print(check_answer("a"))
print(check_answer("a", third_arg=2))
True True
It is important to note that python evaluates the mutable objects of optional arguments once: When the function is defined. This means that if you make the default an empty object, for instance, it will persist across all calls.
This is one of the most common errors for beginners
def f(a, L = []):
L.append(a)
return L
print(f(1))
print(f(2))
print(f(3))
[1] [1, 2] [1, 2, 3]
To re-intialize L to the empty list each time, initialize L to an immultable object instead:
def fnew(a, L=None):
if L is None:
L = []
L.append(a)
return L
print(fnew(1))
print(fnew(2))
print(fnew(3))
[1] [2] [3]
Return Values¶
Functions always return a value of any type. If one is not explicitly given, then they return None.
a = my_fun1(10)
print(a)
in the function, here's i: 10 None
A function can return multiple values using tuple.
def fib2(n): # return Fibonacci series up to n (from the python tutorial)
"""Return a list containing the Fibonacci series up to n."""
result = []
a, b = 0, 1
while a < n:
result.append(a) # see below
a, b = b, a+b
return result, len(result)
#lst, n = fib2(250)
#print(n)
#print(lst)
rv = fib2(250)
print(rv[0], rv[1])
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233] 14
Namespace¶
A function has its own namespace. If a variable is not defined in the function, then it will look for the global variable in its enclosing namespace (such as the module containing the function; more later).
def print_fun(string, n):
if n < global_var:
print(string * n)
else:
print(string * global_var)
print_fun("-", 5)
print_fun("-", 20)
----- ----------
global_var = 100
print_fun("-", 50)
--------------------------------------------------
Docstring¶
Note that this function includes a docstring (just after the function definition). This is used by the help system.
help(fib2)
Help on function fib2 in module __main__:
fib2(n)
Return a list containing the Fibonacci series up to n.
The docstring can also be modified by setting __doc__.
fib2.__doc__
'Return a list containing the Fibonacci series up to n.'
fib2.__doc__ = 'Updated: Return a list containing the Fibonacci series up to n.'
help(fib2)
Help on function fib2 in module __main__:
fib2(n)
Updated: Return a list containing the Fibonacci series up to n.
'Python' 카테고리의 다른 글
| 7. Lambda Functions (0) | 2024.03.14 |
|---|---|
| 6. Recursion vs. Iteration (1) | 2024.03.14 |
| 4. Conditionals and Loops (0) | 2024.03.14 |
| 3. Exception Handling (0) | 2024.03.14 |
| 2. List, Set, Tuple (1) | 2024.03.14 |
