24 Function就是另一種type的object

  • 之前我們看到x = [1,2,3],我們會說x是list; 看到y = {'foo': 42},我們會說y是dictionary
  • 那,看到function時:
def my_func():
  print("hello")
  • my_func是什麼?my_func就是另一種type的object,我們會說my_func是function這種type
type(my_func)
#> <class 'function'>
  • 特別注意,my_func是function物件,但my_func()是去call這個function,得到return value
my_func
#> <function my_func at 0x1346d1ee0>
my_func()
#> hello

24.1 把function assing給一個變數

  • 那既然my_func是一個物件,我就可以把這個物件assign到一個variable:
f = my_func
f()
#> hello

24.2 把function丟到list/dictionary裡面

  • 既然可以把function給assign到一個variable,那我們當然也可以把function加到各種collections裡面
  • 例如,把funciton加到list裡面:
list_of_functions = [my_func, print, sum]
  • 然後取出對應的位子後,call這個function的功能:
list_of_functions[0]()
#> hello
  • 或是把function加到dictionary裡面:
dict_of_funcs = {
  'func1': my_func,
  'func2': print,
  'func3': sum
}
  • 然後,一樣取出對應的element後,再用他的功能:
dict_of_funcs.get('func2')("this is print function")
#> this is print function

24.3 把function當成argument

  • 假設我們現在已經寫好兩個function如下:
def no_docstring_func():
  return(43)

def yes_docstring_func():
  """
  Docstring here hahaha
  """
  return(42)
  • 那我們可以再寫一個function,來判斷某個定義的function是否含有docstring:
def has_docstring(func):
  """
  purpose: 
    check whether `func` has docstring or not
  Args:
    func (callable): A function
  Returns:
    bool
  """
  return func.__doc__ is not None
  • 這個function的argument就是一個function了,來試試看:
print(has_docstring(no_docstring_func))
#> False
print(has_docstring(yes_docstring_func))
#> True