INF1-IP: Week 9

Lecture 16 - Objects 2: Inheritance and polymorphism

 

Learning outcomes
  • Creating a subclass which inherits from another class
  • Extending initialisation and methods with super()
  • Overriding methods and using different objects through the same methods
  • Checking class relationships with isinstance()
  • Marking attributes and methods as private with leading underscores

 

Key terms
Click to expand.

Inheritance

Building a class on another class, reusing and extending its behaviour.

Superclass / base class / parent class

The class from which another class inherits.

Subclass / child class

A class that inherits from a base class.

Composition

Building an object using other objects as its parts or attributes.

Overriding

Defining a method in a subclass with the same name as an inherited method.

Polymorphism

Using the same operation with different kinds of objects, each providing their own suitable behaviour.

Duck typing

Using the operations an object supports without requiring a particular class.

Encapsulation

Keeping state and the operations that manage it together behind a clear interface.

Interface

The operations that other code is expected to use with an object.

 
New syntax
Click to expand.

class SubClass(SuperClass):

Define a subclass of SuperClass.

super().__init__(attributes)

Use the inherited initialiser for the current object.

super().method()

Call the inherited method as part of an overriding method.

isinstance(value, ClassName)

Check whether value is an instance of ClassName or one of its subclasses.

isinstance(value, (ClassName1, ClassName2, ...))

Accept an instance of any class in the tuple, including their subclasses.

_privateName

By convention, marking an attribute or method as private. Does not actually prevent access!

 
Further practice

 


 

Lecture 17 - Collections 3: Copying, searching, and sorting

 

Learning outcomes
  • The difference between comparing equality and identity of objects
  • Using the is operator in Python
  • Creating deep copies using the copy module
  • Implementing linear and binary search and recognising the strengths and limitations of both
  • Using Python's sorted() and .sort(), including using a function as a sort key
  • Tracing and implementing bubble sort and insertion sort
  • Comparing algorithms by counting comparisons and movements

 

Key terms
Click to expand.

Alias

Another name or reference to the same object.

Equality

Whether two names refer to the same values, tested with ==.

Identity

Whether two names refer to the very same object in memory, tested with is.

Shallow copy

A new outer collection that shares its contained objects with the original.

Deep copy

A collection copy that also copies nested mutable objects.

Linear search

Inspecting items in sequence until a match is found or the collection ends.

Binary search

Searching a sorted sequence by repeatedly discarding half of the remaining region.

Stable sort

A sort that preserves the relative input order of items with equal keys.

Bubble sort

Repeatedly comparing and swapping neighbouring items, placing the largest remaining item at the end of each pass.

Insertion sort

Growing a sorted section by moving earlier values to make room for the next item.

Algorithm time complexity

How the time an algorithm needs changes with its input.

 
New syntax
Click to expand.

a is b, a is not b

Tests whether two references identify the same or objects.

copy.deepcopy(collection)

Copies collection and its nested mutable objects, after importing copy.

items.index(value)

Finds the first matching item's index in a list; raises ValueError if absent.

sorted(items), list.sort()

Creates a sorted collection, and sorts an existing list in place, respectively. The latter returns None.

sorted(records, key=key_function)

Sorts records using key_function to determine which part of each record to use as the key.

left, right = right, left

Uses unpacking assignment to swap two values after evaluating the right-hand side.

 

Further reading

 


 

Tutorial

Tutorial 6: The Worst Language Model combines (almost) everything we've learned about Python tooling so far as you'll download and use a tiny language model on your own laptop. What could go wrong?

  • 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

No assessment this week.

 


 

License
All rights reserved The University of Edinburgh