Aug 26: PEP 8 Style Guide, flake8
Learning Objectives
After today's class, you should be able to:
- Explain what a PEP is and why PEPs are important.
- Run flake8 to check your code before submitting.
- Describe at least three style guidelines for Python.
Mini Lecture¶
Announcements [10 min]
- See the Help page:
- TA Hours (Sun–Thu) and Office Hours (Mon–Fri)
- Ask and answer questions on Piazza (anytime)
- Due tomorrow
- Due Wednesday
- Syllabus Quiz on Canvas (must get 5/5 = 100%)
- HW1 Reflection on Canvas
Python Style Guide [10 min]
- PEP = Python Enhancement Proposal
- PEP 8 – Style Guide for Python Code
- Prettier version: https://pep8.org/
- PEP 257 – Docstring Conventions
- Real-world example: Google's style guide
- PEP 8 – Style Guide for Python Code
- Check your code before submitting to Gradescope!
Mini Exercise¶
Fixing Style [20 min]
- Download payroll.py and setup.cfg
- Run payroll.py first to see how it works
- Run
!flake8 payroll.py
in the shell- You should see several lines of output
payroll.py:4:18
means "Line 4, Char 18"
- Correct each issue, one by one, in the editor
- Rerun flake8 by pressing Up+Enter in the shell
- If you finish early, delete setup.cfg and go all the way PEP 8
- Our config does not enforce all of PEP 8's requirements
Thonny Tips¶
Quick Demo [10 min]
- Have you discovered the View menu in Thonny?
- Consider showing "Files" and "Variables"
- Other windows might be useful to you
- Tools > Options… Theme & Font
- UI theme: Clean Dark
- Syntax theme: Tomorrow Night Eighties
- Feel free to choose the ones you want!