Skip to content

Function

Source

https://pythonbook.cc/chapters/basic/function

引數

source: https://docs.python.org/3/library/functions.html#sorted

python
sorted(iterable, /, *, key=None, reverse=False)
  Return a new sorted list from the items in iterable.
  • / 前的 iterable 一定要用位置引數
  • * 後的 keyreverse 一定要用關鍵字引數
  • example:
    python
    sorted([9, 5, 2, 7], reverse=True)

參數預設值的坑

source: https://pythonbook.cc/chapters/basic/function#參數預設值

python
def add_to_box(a, b, box=[]):
  box.append(a)
  box.append(b)
  return box

add_to_box(1, 4) # [1, 4]
add_to_box(5, 0) # [1, 4, 5, 0]

可以調整為:

python
def add_to_box(a, b, box=None):
  if box is None:
      box = []
  box.append(a)
  box.append(b)
  return box

add_to_box(1, 4) # [1, 4]
add_to_box(5, 0) # [5, 0]

lambda

python
add = lambda a, b: a + b
add(1, 2) # 3

Closure

運作原理 source:https://pythonbook.cc/chapters/basic/function-advanced#運作原理