Docstrings

Python Tingkat Menengah untuk Pengembang

Jasmin Ludolf

Senior Data Science Content Developer

Docstrings

  • String (blok teks) yang menjelaskan fungsi

  • Membantu pengguna memahami cara memakai fungsi

Help on built-in function round in module builtins:

round(number, ndigits=None)

Round a number to a given precision in decimal digits. The return value is an integer if ndigits is omitted or None. Otherwise the return value has the same type as the number. ndigits may be negative.
Python Tingkat Menengah untuk Pengembang

Mengakses docstring

# Akses informasi, termasuk docstring
print(help(round))
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number.  ndigits may be negative.

None
Python Tingkat Menengah untuk Pengembang

Mengakses docstring

# Hanya akses docstring
print(round
Python Tingkat Menengah untuk Pengembang

Mengakses docstring

# Hanya akses docstring
print(round.
Python Tingkat Menengah untuk Pengembang

Mengakses docstring

# Hanya akses docstring
print(round.__
Python Tingkat Menengah untuk Pengembang

Mengakses docstring

# Hanya akses docstring
print(round.__doc
Python Tingkat Menengah untuk Pengembang

Mengakses docstring

# Hanya akses docstring
print(round.__doc__)
Round a number to a given precision in decimal digits.

The return value is an integer if ndigits is omitted or None.  Otherwise
the return value has the same type as the number.  ndigits may be negative.

$$

$$

  • .__doc__: atribut "dunder-doc"
Python Tingkat Menengah untuk Pengembang

Membuat docstring

def average(values):

# Docstring satu baris """Hitung mean dari deret nilai dan bulatkan ke dua angka desimal."""
average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Python Tingkat Menengah untuk Pengembang

Mengakses docstring

# Akses docstring kita
print(average.__doc__)
Find the mean in a sequence of values and round to two decimal places.
Python Tingkat Menengah untuk Pengembang

Memperbarui docstring

# Perbarui docstring fungsi
average.__doc__ = "Calculate the mean of values in a data structure, rounding the results to 2 digits."


print(help(average))
Help on function average in module __main__:

average(values)
    Calculate the mean of values in a data structure, rounding the results to 2 digits.
Python Tingkat Menengah untuk Pengembang

Docstring multi-baris

def average(values):
    """
    Find the mean in a sequence of values and round to two decimal places.






    """

average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Python Tingkat Menengah untuk Pengembang

Docstring multi-baris

def average(values):
    """
    Find the mean in a sequence of values and round to two decimal places.

    Args:




    """

average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Python Tingkat Menengah untuk Pengembang

Docstring multi-baris

def average(values):
    """
    Find the mean in a sequence of values and round to two decimal places.

    Args:
        values (list): A list of numeric values.



    """

average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Python Tingkat Menengah untuk Pengembang

Docstring multi-baris

def average(values):
    """
    Find the mean in a sequence of values and round to two decimal places.

    Args:
        values (list): A list of numeric values.

    Returns:
        rounded_average (float): The mean of values, rounded to two decimal places.
    """

average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Python Tingkat Menengah untuk Pengembang

Mengakses docstring

# Bantuan
print(help(average))
Help on function average in module __main__:

average(values)
    Find the mean in a sequence of values and round to two decimal places.

        Args:
            values (list): A list of numeric values.

        Returns:
            rounded_average (float): The mean of values, rounded to two decimal places.
Python Tingkat Menengah untuk Pengembang

Ayo berlatih!

Python Tingkat Menengah untuk Pengembang

Preparing Video For Download...