เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

PEP 8 กับการเขียน documentation

ที่ผ่านมาเราได้ดู PEP 8 ในส่วนของโค้ดที่ใช้งานจริง แต่ PEP 8 ยังมีกฎสำหรับการเขียน comment และ documentation ให้อ่านง่ายขึ้นด้วย ในแบบฝึกหัดนี้ ให้แก้ไข comment ประเภทต่าง ๆ ให้ถูกต้องตามมาตรฐาน PEP 8

ผลลัพธ์จากการตรวจสอบสไตล์โค้ดด้วย pycodestyle แสดงอยู่ด้านล่าง

my_script.py:2:15: E261 at least two spaces before inline comment
my_script.py:5:16: E262 inline comment should start with '# '
my_script.py:11:1: E265 block comment should start with '# '
my_script.py:13:2: E114 indentation is not a multiple of four (comment)
my_script.py:13:2: E116 unexpected indentation (comment)

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

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

ดูคอร์ส

คำแนะนำการฝึกหัด

  • ใช้ผลลัพธ์จาก pycodestyle เป็นแนวทางในการแก้ไข comment ในโค้ดให้ถูกต้องตามมาตรฐาน PEP 8

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

def print_phrase(phrase, polite=True, shout=False):
    if polite:# It's generally polite to say please
        phrase = 'Please ' + phrase

    if shout:  #All caps looks like a written shout
        phrase = phrase.upper() + '!!'

    print(phrase)


#Politely ask for help
print_phrase('help me', polite=True)
 # Shout about a discovery
print_phrase('eureka', shout=True)
แก้ไขและรันโค้ด