{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module 2, Practical 1\n", "\n", "The practicals of the second teaching module are a refinement of those prepared by Massimiliano Luca and Erik Dassi. Many thanks for their help.\n", "\n", "## Object Oriented Programming\n", "\n", "As seen in the lecture, Python is a multi-paradigm language and it supports in fact the **imperative/procedural** paradigm (programs are sequences of statements that change the state of the system), the **functional** paradigm (programs are seen as mathematical functions, e.g. list comprehensions), some libraries are **declarative** (they define the logic without specifying the control-flow e.g. Matplotlib) but is also **Object Oriented**. In fact **everything in Python is an object.** Moreover, as we will see, new data-types can be defined in Python.\n", "\n", "In Object Oriented Programming (OOP) objects are **data structures that contain data, which is attributes and functions to work with them**. In OOP, programs are made by a set of objects that interact with each other.\n", "\n", "OOP allows to create a distinction (*abstraction*) between the way objects are **implemented** and how objects are **used** (i.e. what we can do with them).\n", "\n", "### Classes, Methods and Objects\n", "\n", "The three key players of OOP are: **classes**, **objects** and **methods**. \n", "\n", "**Classes** (types) are an abstraction that captures: \n", "\n", "1. the internal data representation (i.e. data attributes that are called **fields**)\n", "2. the interface to interact with the class (i.e. functions that can be used to manipulate the the **methods**). \n", "\n", "\n", "**Objects** are **instances** of classes. Classes define the structure and are used to create objects. Objects are a *concrete* realization of the class, a real instance based on the footprint of the class. Programs are interactions among different objects. \n", "\n", "\n", "**Methods** are functions that can be applied to manipulate objects.\n", "\n", "Attributes and methods within an instantiated object can be accessed by using the ```. ``` (dot) operator.\n", "\n", "\n", "### Self\n", "Within a class method, we can refer to that very same instance of the object being created by using a special argument that is called ```self```. **self** is always the first argument of each method.\n", "\n", "\n", "