Docstrings

การเขียนฟังก์ชันใน Python

Shayne Miel

Software Architect @ Duo Security

ฟังก์ชันที่ซับซ้อน

def split_and_stack(df, new_names):
  half = int(len(df.columns) / 2)
  left = df.iloc[:, :half]
  right = df.iloc[:, half:]
  return pd.DataFrame(
    data=np.vstack([left.values, right.values]),
    columns=new_names
  )
การเขียนฟังก์ชันใน Python
def split_and_stack(df, new_names):
  """Split a DataFrame's columns into two halves and then stack
  them vertically, returning a new DataFrame with `new_names` as the
  column names.

  Args:
    df (DataFrame): The DataFrame to split.
    new_names (iterable of str): The column names for the new DataFrame.

  Returns:
    DataFrame
  """
  half = int(len(df.columns) / 2)
  left = df.iloc[:, :half]
  right = df.iloc[:, half:]
  return pd.DataFrame(
    data=np.vstack([left.values, right.values]),
    columns=new_names
  )
การเขียนฟังก์ชันใน Python

โครงสร้างของ docstring

def function_name(arguments):
  """
  Description of what the function does.

  Description of the arguments, if any.

  Description of the return value(s), if any.

  Description of errors raised, if any.

  Optional extra notes or examples of usage.
  """
การเขียนฟังก์ชันใน Python

รูปแบบของ docstring

  • Google Style
  • Numpydoc
  • reStructuredText
  • EpyText
การเขียนฟังก์ชันใน Python

Google Style - คำอธิบายฟังก์ชัน

def function(arg_1, arg_2=42):
  """Description of what the function does.
  """
การเขียนฟังก์ชันใน Python

Google Style - อาร์กิวเมนต์

def function(arg_1, arg_2=42):
  """Description of what the function does.

  Args:
    arg_1 (str): Description of arg_1 that can break onto the next line
      if needed.
    arg_2 (int, optional): Write optional when an argument has a default
      value.
  """
การเขียนฟังก์ชันใน Python

Google Style - ค่าที่คืนกลับ

def function(arg_1, arg_2=42):
  """Description of what the function does.

  Args:
    arg_1 (str): Description of arg_1 that can break onto the next line
      if needed.
    arg_2 (int, optional): Write optional when an argument has a default
      value.

  Returns:
    bool: Optional description of the return value
    Extra lines are not indented.
  """
การเขียนฟังก์ชันใน Python
def function(arg_1, arg_2=42):
  """Description of what the function does.

  Args:
    arg_1 (str): Description of arg_1 that can break onto the next line
      if needed.
    arg_2 (int, optional): Write optional when an argument has a default
      value.

  Returns:
    bool: Optional description of the return value
    Extra lines are not indented.

  Raises:
    ValueError: Include any error types that the function intentionally
      raises.

  Notes:
    See https://www.datacamp.com/community/tutorials/docstrings-python
    for more info.  
  """
การเขียนฟังก์ชันใน Python

Numpydoc

def function(arg_1, arg_2=42):
  """
  Description of what the function does.

  Parameters
  ----------
  arg_1 : expected type of arg_1
    Description of arg_1.
  arg_2 : int, optional
    Write optional when an argument has a default value.
    Default=42.

  Returns
  -------
  The type of the return value
    Can include a description of the return value. 
    Replace "Returns" with "Yields" if this function is a generator.
  """
การเขียนฟังก์ชันใน Python

การดึง docstring

def the_answer():
  """Return the answer to life, 
  the universe, and everything.

  Returns:
    int
  """
  return 42

print(the_answer.__doc__)
Return the answer to life, 
  the universe, and everything.

  Returns:
    int
import inspect
print(inspect.getdoc(the_answer))
Return the answer to life, 
the universe, and everything.

Returns:
  int
การเขียนฟังก์ชันใน Python

มาฝึกกันเถอะ!

การเขียนฟังก์ชันใน Python

Preparing Video For Download...