INF1-IP: Week 4
Lecture 6 - Functions
- Lecture slides
- Self-guided notebook (right-click → Save As... then remove the .txt part so it is saved as an .ipynb)
Learning outcomes
- Recognising how the IPO pattern is present in Python's built-in functions
- Defining and calling functions with parameters and return values
- Supplying positional and keyword arguments, and choosing default values
- Documenting functions with docstrings
- Understanding
Noneand variable scope
Key terms
Click to expand.
Function
A reusable piece of code which can receive arguments and return a value.
Parameter
A name in a function definition which receives an argument when the function is called.
Argument
A value supplied to a function call as input.
Caller
The piece of code that calls a function and receives its returned value.
Return value
The value a function sends back to the caller. A function without an explicit return value returns
None.Positional argument
An argument matched to a parameter by its position in the call.
Keyword argument
An argument matched to a parameter by its name.
Default value
A value used for a parameter when the caller omits that argument.
Docstring
A string at the beginning of a function body which documents its purpose and use. Conventionally written using triple quotes.
Scope
The part of a program in which a name can be used.
Globally scoped variables
Variables that can be used anywhere in a program or notebook.
Locally scoped variables
Variables that can only be used inside a block. For example, parameters and variables declared inside of a function are locally scoped to that function.
New syntax
Click to expand.
min(values),max(values), andsum(values)Returns the smallest value in, largest value in, and total of
values, respectively.def name(parameter):Defines a function with an indented body. The body runs when the function is called and given access to any parameters.
return valueEnds the current function call and gives
valueback to its caller.NoneA special value representing the absence of a result. Returned when a function does not otherwise returns a value.
NoneTypeA special type for the
Nonevalue.def name(parameter=default):Gives a parameter a default value, making it optional. Make sure to write required parameters before parameters with defaults.
name(parameter=value)Supplies an argument by keyword when calling a function.
'''Text.'''and"""Text."""Denotes a multi-line string. By convention, the double-quoted variant is also used to write a docstring as the first statement in a function body.
help(function)Displays information about a function, including its parameters and docstring.
NameErrorA type of error caused by using a name that is not available in the current scope.
Further practice
- Official Python tutorial: Defining functions, Default argument values, Keyword arguments, and Documentation strings
- The sections on special parameters and arbitrary argument lists are beyond this lecture.
- W3Schools: Functions and Function arguments
- Helsinki Python MOOC: Defining functions and More functions
Lecture 7 - Objects 1: Classes and instances
- Lecture slides
- Self-guided notebook (right-click → Save As... then remove the .txt part so it is saved as an .ipynb)
Learning outcomes
- Understanding the relationship between a class and its instances
- Defining classes and setting initial values with
__init__ - Reading and changing instance attributes through methods
- Choosing between class attributes and instance attributes
- Deleting attributes and variables using
del - Using
__str__to give an object a readable description
Key terms
Click to expand.
Object
A data structure that has a value and methods associated with it.
Class
A definition of a type of object, including its data and behaviour.
Instance
A particular object belonging to a class.
Instantiation
Creating an instance of a class.
Constructor
The method that handles instantiation; in Python,
__init__.Attribute
A named value associated with an object or a class, accessed using a dot
..Instance attribute
An attribute stored on an individual instance.
Class attribute
An attribute stored on the class, available to instances which do not have their own attribute of that name.
Method
A function defined in a class. An instance method receives the instance as its first argument, with the parameter usually named
self.Magic or dunder methods
Special methods available to classes denoted using double underscores, including
__init__and__str__.
New syntax
Click to expand.
class Name:Begins a class definition with an indented body. By convention, class names are capitalised.
def __init__(self, ...):Defines the method which initialises a newly created instance.
selfThe conventional name for the first parameter of an instance method, referring to the instance receiving the call.
self.attribute = valueStores an attribute on the current instance.
instance = ClassName(arguments)Creates and initialises an instance of the class.
instance.attributeAccesses an attribute using dot notation.
instance.method(arguments)Calls a method, passing
instanceas the first argument of.method().def __str__(self):Defines the method which returns a readable string representation of an instance.
delRemoves a name, such as a variable or an attribute. The object it referred to survives if another name still refers to it.
Further practice
- Official Python tutorial: A first look at classes
- Inheritance will be covered in Lecture 16.
- W3Schools: OOP - all sections up to and including Class methods.
- Helsinki Python MOOC: Classes and objects, Defining classes, and Defining methods.
- Type annotations, which the MOOC uses, will be covered in Lecture 14.
Tutorial
This week's tutorial time will be used for Assignment 1.
Assessment
Assignment 1 will take place on Friday (16 October), during tutorial time.
For details on the assignment, see Assessment > Assignment 1: Programming Essentials on Learn.