เอกสารประกอบโค้ด

หลักการวิศวกรรมซอฟต์แวร์ใน Python

Adam Spannbauer

Machine Learning Engineer at Eastman

เอกสารประกอบโค้ดใน Python

  • คอมเมนต์
# Square the number x
  • Docstrings
    """Square the number x

    :param x: number to square
    :return: x squared

    >>> square(2)
    4
    """
หลักการวิศวกรรมซอฟต์แวร์ใน Python

คอมเมนต์

# This is a valid comment
x = 2
y = 3  # This is also a valid comment
# You can't see me unless you look at the source code

# Hi future collaborators!!
หลักการวิศวกรรมซอฟต์แวร์ใน Python

การเขียนคอมเมนต์ที่มีประสิทธิภาพ

การคอมเมนต์แบบ 'what'

# Define people as 5
people = 5

# Multiply people by 3
people * 3

การคอมเมนต์แบบ 'why'

# There will be 5 people attending the party
people = 5

# We need 3 pieces of pizza per person
people * 3
หลักการวิศวกรรมซอฟต์แวร์ใน Python

Docstrings

def function(x):
    """High level description of function

    Additional details on function
หลักการวิศวกรรมซอฟต์แวร์ใน Python

Docstrings

def function(x):
    """High level description of function

    Additional details on function

    :param x: description of parameter x
    :return: description of return value

ตัวอย่างหน้าเว็บที่สร้างจาก docstring ใน Flask package.

หลักการวิศวกรรมซอฟต์แวร์ใน Python

Docstrings

def function(x):
    """High level description of function

    Additional details on function

    :param x: description of parameter x
    :return: description of return value

    >>> # Example function usage
    Expected output of example function usage
    """
    # function code
หลักการวิศวกรรมซอฟต์แวร์ใน Python

ตัวอย่าง docstring

def square(x):
    """Square the number x

    :param x: number to square
    :return: x squared

    >>> square(2)
    4
    """
    # `x * x` is faster than `x ** 2`
    # reference: https://stackoverflow.com/a/29055266/5731525
    return x * x
หลักการวิศวกรรมซอฟต์แวร์ใน Python

ผลลัพธ์ของตัวอย่าง docstring

help(square)
square(x)
    Square the number x

    :param x: number to square
    :return: x squared

    >>> square(2)
    4
หลักการวิศวกรรมซอฟต์แวร์ใน Python

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

หลักการวิศวกรรมซอฟต์แวร์ใน Python

Preparing Video For Download...