INF1-IP: Week 8
Lecture 14 - Code quality 2: Community conventions
- Lecture slides
- Self-guided notebook (right-click → Save As... then remove the .txt part so it is saved as an .ipynb)
Learning outcomes
- Python's style conventions for variable names and constants;
- recognising function side effects and avoiding shared mutable defaults; and
- documenting the expected types of arguments and return values with type hints.
Key terms
Click to expand.
Constant
A named value intended to remain unchanged. In Python, constants aren't checked, but conventionally written with capital letters.
Pure function
A function whose result depends only on its arguments and which has no side effects.
Side effect
A change beyond a returned value, such as output or mutation of a supplied object.
Mutable default
A changeable object used as a default parameter value. Python creates it once, when the function is defined, so every call that omits the argument shares it, and changes persist between calls.
Function contract
The expected inputs, results, and other behaviour of a function.
Type hint / Type annotation
Documentation in the code of expected types. Not automatically enforced by Python.
New syntax
Click to expand.
CONSTANT = 1An uppercase name marks a constant by convention.
def function(argument: type) -> type:Annotates a parameter and a return value with their expected types.
list[type],dict[type, type],tuple[type, type]Describes the contents of collections in type hints.
type1 | type2A type hint allowing multiple possible types (Python 3.10 and later).
-> NoneDocuments that a function has no result for the caller to use.
Further practice
- Python Enhancement Proposals: some relevant ones include PEP 1 - PEP Purpose and Guidelines, PEP 8 - Style Guide for Python Code, PEP 20 - The Zen of Python, and PEP 257 - Docstring Conventions.
- Official Python tutorial: Function annotations and Default argument values.
- W3Schools: no relevant pages this time!
- Helsinki Python MOOC: The type of the argument.
Lecture 15 - A look at other programming languages
- Lecture slides
- Self-guided notebook (right-click → Save As... then remove the .txt part so it is saved as an .ipynb)
Learning outcomes
- why there are so many programming languages, and what problems each was designed to solve;
- the difference between a language, its implementation, and the machine that runs it;
- compiled versus interpreted languages, and static versus dynamic typing;
- imperative, functional, and declarative ways of solving the same problem;
- why a task can be short in one language and long in another;
- choosing a language for a problem, and justifying that choice; and
- which of your skills will transfer to a language you have never seen before.
Key terms
Click to expand.
Programming language
A notation with a defined syntax, a defined meaning for everything you can write, and a machine that can carry that meaning out.
Domain-specific language
A language designed for a particular area, such as questions about tables (SQL) or patterns in text (regular expressions).
Implementation
A program that runs code written in a particular language. CPython is one implementation of Python, written in C.
Compiler
A program that translates code into another representation, such as machine code or bytecode.
Interpreter
A program that carries out instructions expressed in source code or an intermediate representation.
Semantics
What a piece of code means, as opposed to its syntax, which is what you are allowed to type.
Type system
The rules a language uses to decide which operations are allowed on which values.
Static typing
Checking type rules before execution. This catches many incompatible operations early, but it does not prevent every runtime failure or incorrect result.
Dynamic typing
Checking the type rules while the program runs, so that type errors only appear when the line containing them is reached.
Paradigm
The style of solution a language makes easy. Imperative code describes steps, functional code describes values built from other values, and declarative code describes the result and leaves the method to the engine.
Overflow
What happens when a result is too big for the fixed number of bits used to store it. NumPy's
int64arrays wrap in this example; other types and languages may have different rules.Esoteric language
A language designed to make a point or a joke, rather than to be used for real work, such as Chef, Shakespeare, or Piet.
Further reading
- Official Python documentation: Regular expressions, if you would like to learn more about them.
- Helsinki Python MOOC: Regular expressions, if you would like to learn more about them.
- NumPy: the absolute basics for beginners - read the introduction to arrays if you want to explore the numerical comparison in Section 5.
- The Esoteric Languages Wiki - all the esoteric languages you can dream of!
- Alan J. Perlis, "Special Feature: Epigrams on programming", ACM SIGPLAN Notices 17(9), 1982, 7–13, doi:10.1145/947955.1083808.
Tutorial
Tutorial 5: Bug Your Own Tutorial has you explore VS Code's Python debugger. You'll create a worksheet that gives others in your tutorial a broken project that they'll have to fix using the debugger and fix another team's yourself.
- Interactive notebook (right-click → Save As... then remove the .txt part so it is saved as an .ipynb)
- Note that this notebook was created to be used in Notable; its interactive checker cells might not work in every environment.
Assessment
Class Test 2 will take place on Thursday 12th November in AT6:06. Check your timetable to see which session you are scheduled for.
For details, see Assessment.