INF1-IP: Week 4

Lecture 6 - Functions

 

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 None and 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), and sum(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 value

Ends the current function call and gives value back to its caller.

None

A special value representing the absence of a result. Returned when a function does not otherwise returns a value.

NoneType

A special type for the None value.

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.

NameError

A type of error caused by using a name that is not available in the current scope.

 

Further practice

 


 

Lecture 7 - Objects 1: Classes and instances

 

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.

self

The conventional name for the first parameter of an instance method, referring to the instance receiving the call.

self.attribute = value

Stores an attribute on the current instance.

instance = ClassName(arguments)

Creates and initialises an instance of the class.

instance.attribute

Accesses an attribute using dot notation.

instance.method(arguments)

Calls a method, passing instance as the first argument of .method().

def __str__(self):

Defines the method which returns a readable string representation of an instance.

del

Removes a name, such as a variable or an attribute. The object it referred to survives if another name still refers to it.

 

Further practice

 


 

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.

 


 

License
All rights reserved The University of Edinburgh