def greet():
"""Greeeting"""
print("Hello World")
10 Functions
10.1 Define a Function
greet()#> Hello World
10.2 Arguments
Terminology: key
= value
- Parameters are like
key
- Arguments are like
value
that supply to the parameters
def greet(first, last):
"""Greeeting someone"""
print(f"Hello {first} {last}")
print("Welcome aboard!")
Parameters = first
, last
"Ada", "Lovelace")
greet(#> Hello Ada Lovelace
#> Welcome aboard!
# Using Keyword args
= "Mary",last="John")
greet(first #> Hello Mary John
#> Welcome aboard!
Default parameter must come after required parameter
def increment(x, by = 1):
return x + by
2)
increment(#> 3
2, 2)
increment(#> 4
Arguments = “Ada”, “Lovelace”
10.2.1 Args Type Hint
For Python 3.9 and earlier:
from typing import Union, List
def num_or_list(x: Union[float, List[float]]) -> None:
if isinstance(x, float):
print("x is float")
elif isinstance(x, list):
print("x is list")
For Python 3.10 and later:
def num_or_list(x: float | list[float]) -> None:
if isinstance(x, float):
print("x is float")
elif isinstance(x, list):
print("x is list")
10.3 Return
return a value with return
statement
def get_greet(name):
return f"Hello {name}"
print(get_greet("mom"))
#> Hello mom
By default function return None
, if not provide return value.
def myfun():
print("Hi")
print(myfun())
#> Hi
#> None
10.4 Scoping
Variables are scoped at the function level
This is similar to R
.
# Global Var
= "Joy", 10 name, age
def greet(name = "mark"):
= 20
age return {"name": name, "age": age}
greet()#> {'name': 'mark', 'age': 20}
name#> 'Joy'
age#> 10
10.5 xargs
use *args
to receives any number of arguments to the function.
Ref: official doc
def multi(*numbers):
return numbers
= multi(1, 3)
a
a#> (1, 3)
type(a)
#> <class 'tuple'>
xargs is a tuple. Let’s iterate over “tuple”
def multi(*numbers):
for number in numbers:
print(number)
Must be positional argument only
1, 2, 3)
multi(#> 1
#> 2
#> 3
keyword args is not allowed
= 1)
multi(a #> TypeError: multi() got an unexpected keyword argument 'a'
10.5.1 Exercise
Let’s multiply any input of numbers.
def multiply(*numbers):
= 1
total for number in numbers:
*= number
total return(total)
2, 3, 4)
multiply(#> 24
Same logic using R
<- function(...) {
multiply_r1
<- list(...)
numbers <- 1
total for (number in numbers) {
<- total * number
total
}
total
}
multiply_r1(1, 2, 3)
#> [1] 6
Or using R
functional programming by base::Reduce()
or purrr::reduce()
<- function(...) Reduce(`*`, list(...))
multiply_r2
multiply_r2(1, 2, 3)
#> [1] 6
10.6 xxargs
Use **args
to capture any number of keyword arguments as dictionary.
def save_user(**user):
return user
= save_user(id = 1, name = "mike")
usr
usr#> {'id': 1, 'name': 'mike'}
type(usr)
#> <class 'dict'>
"id"]
usr[#> 1
"name"]
usr[#> 'mike'
If supply positional arg will fail.
"mike")
save_user(#> TypeError: save_user() takes 0 positional arguments but 1 was given