{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Super-minimal Python\n", "\n", "## What is \"Python\"\n", "\n", "When people talk about \"Python\" they usually mean four things. (1) The python language itself, (2) the python software that we use e.g. to execute a script ```python myscript.py```, (3) the python functions and modules that come automatically with Python, (4) the external packages that we can install to achieve specific tasks (e.g working with matrices, images etc.)\n", "\n", "We are going to now briefly review the essential python *language* needed for this course. This includes variables, some containers, flow control, functions and modules. Most of the more complex operations we are going to use come from external packages that we are going to explore in detail later. **Be aware that this is not an exhaustive Python description, but really the bare minimum needed for this course**. If you decide to seriously use Python for image processing, it's probably good to once read more in detail about Python basics. We can recommend for example Jake VanderPlas' book [A Whirlwind Tour of Python](https://jakevdp.github.io/WhirlwindTourOfPython/) entirely available as interactive notebooks. \n", "\n", "## Variables\n", "\n", "Like any other programming language, Python works with variables: as we do in algebra, instead of directly working with numbers we work with symbols that can take different values. So we can just write:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "a = 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And then re-use the variable ```a``` in our program, or here in our notebook. For example:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3*a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dynamic typing\n", "\n", "Unlike e.g. C++ where you have to say in advance what type your variable has (integer, float, string etc.) Python does that for you by *interpreting* your intensions. This is useful and makes writing code much simpler, **but remember that this can be the source of bugs.** Let's create a few variables:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "a = 3\n", "b = 1.2\n", "c = 'my text'\n", "d = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use the ```type``` function to get the variable type:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(b)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(c)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that if we start mixing variables, Python is also going to interpret the resulting type for us:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "e = a * b" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple operations\n", "As already seen above you can do simple mathematics with variables as signs such as ```+``` and ```*``` are understood by Python.\n", "\n", "In addition to those, you can also perform logical operations with boolean variables. A boolean can be ```True``` or ```False``` and is typically generated via a comparison:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a > b" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_boolean = a > b\n", "my_boolean" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_boolean2 = e == a\n", "my_boolean2" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_boolean and my_boolean2" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_boolean or my_boolean2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will see that booleans are helpful when dealing with image masks." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions\n", "Python functions are just like mathematical functions $y = f(x)$, they have a name ```f```, take an input ```x``` and give an output ```y```. We have just seen an example of such a function above with ```type()```. Function definitions can come from different places:\n", "\n", "### Built-in functions\n", "These functions are directly implemented in Python and you don't have to do anything special to use them. You can find a list [here](https://docs.python.org/3/library/functions.html). For example, you can take an absolute value:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = -3\n", "b = abs(a)\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Module functions\n", "All additional functions come from modules. Those are separate software packages logically organised by domains (handling files, doing maths etc.) that one can import in a program. There are two types of modules: some are native to Python (no installation necessary) and some are distributed externally (and need to be installed using e.g. pip or conda).\n", "\n", "Let's imagine we want to know the content of folders e.g. to execute some program on all images within a folder. We can use for that the ```os``` package. We import it using:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "import os" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then all functions of the ```os``` module are available using the dot notations:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/Users/gw18g940/OneDrive - Universitaet Bern/Courses/BiaPy/BIAPy'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mypath = os.getcwd()\n", "mypath" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ```getcwd()``` function gives us the current path. In this particular case, there is no input, just an output in the form of a path string.\n", "\n", "Some functions that logically belong together are also sometimes grouped together into sub-modules, that one can call again using the \"dot noation\". For example we can do all sorts of operations on path strings using the functions grouped in the ```os.path``` sub-module:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'BIAPy'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.basename(mypath)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function above takes a path as input and returns the last part of the path.\n", "\n", "External packages that we installed ourselves are imported in the exact same way. For example we are going to use a lot Numpy, which allows us to do matrix computations:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "import numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that to abbreviate code, it is very common to abbreviate the name of imported packages. This can be done like this:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that in some cases, sub-modules need to be imported separately. For example the import/export module of the image processing package scikit-image:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "import skimage.io" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Your own function\n", "\n", "If is very easy to create a function in Python. For example the minimum you need to define a parabola function ($f(x) = x^2$) is:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "def parabola(x):\n", " y = x**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```def``` indicates that we define a parabola. The ```parabola``` *name* is chosen by us. Like in algebra we have a parenthesis with *variables*, here ```x```. Then we have a column ```:``` and everything that follows it and which is **indented** belongs to the *function body*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can then use the function *name* and pick a parameter value of our choice:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "result = parabola(3)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result is None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see that the last problem we have is that the function doesn't return any value. It does the computation but keeps it hidden from us. The solution to this is to explicitly say what we want to ```return```:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "def parabola(x):\n", " y = x**2\n", " return y" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "result = parabola(3)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can naturally have more that one parameter to pass to the function (e.g. $f(x,a, b) = a * x^2+b$). Parameters can also have a default value as shown here, where the parameter ```b == 2```:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "def parabola2(x, a, b=2):\n", " y = a * x**2 + b\n", " return y" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "parabola2(3,0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The default can of course be overriden by setting an explicit value:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "parabola2(3,0,10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Containers\n", "\n", "Beyond simple variables, Python offers several types of containers that can use multiple variables. \n", "\n", "### Lists \n", "The simplest one is the list, enclosed by ```[]```:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "mylist = [1,6,10,20]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A list can contain any type of variable, and you can even mix them, even though that happends rarely:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "mylist_mix = [1, 6.8, 'this is my text',[9,10,3]]" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 6.8, 'this is my text', [9, 10, 3]]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist_mix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionaries\n", "Dictionaries are pairs sets of pairs of definitions and values, like an actual dictionary. They are creted in the following way:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'apple', 'weight': 100, 'diameter': 10}" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mydict = {'name': 'apple', 'weight': 100, 'diameter': 10}\n", "mydict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionaries are useful to reference properties of objects e.g. the geometric properties of segmented objects in an image." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing/modifying list elements\n", "Elements of both lists and dictionaries can be accessed using square bracktets ```[]```. List elements are selectable via a *positional index* (starting at 0), e.g. if you want to access the third element you do:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'this is my text'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist_mix[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And dictionary elements can be accessed via the definitions or keys:" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mydict['weight']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also modify an element in the same way:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 6.8, 'new text', [9, 10, 3]]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist_mix[2] = 'new text'\n", "mylist_mix" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'apple', 'weight': 200, 'diameter': 10}" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mydict['weight'] = 200\n", "mydict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Copies\n", "\n", "**Beware that simple copies of lists and dictionaries are not independent copies!**:" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "mylist_mix2 = mylist_mix\n", "mylist_mix[0] = 'I changed this'" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['I changed this', 6.8, 'new text', [9, 10, 3]]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist_mix" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['I changed this', 6.8, 'new text', [9, 10, 3]]" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist_mix2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To create a **true independent copy** you can use the ```copy()``` methods on your list:" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "mylist_mix2 = mylist_mix.copy()" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "mylist_mix[0] = 'again'" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['again', 6.8, 'new text', [9, 10, 3]]" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist_mix" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['I changed this', 6.8, 'new text', [9, 10, 3]]" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist_mix2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python variables are objects\n", "\n", "If you are familiar with programming you probably know about object oriented programming. If not, just know that in programming an object can have \"features\" (attributes) and \"functionalities\" (methods). Since every Python variable is an object, it means that each variable, on top of its *value*, can contain additional information. Let's look at a practical example. We define an complex number:" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "complex" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "imaginary = 3 + 5j\n", "type(imaginary)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Even this simple variable has both *attributes* and *methods*. For example we can recover the real and imaginary parts of the number through the attributes ```real``` and ```img```. This is done with the *dot-notation*:" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "imaginary.real" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "imaginary.imag" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Methods* are accessed with the same *dot-notation*. However since they are actual functions, we need to use parentheses. For example, we can create a string variable and then use it's ```count()``` method to count the number of occurrences of a given letter:" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "this is my string\n" ] }, { "data": { "text/plain": [ "3" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mystring = 'this is my string'\n", "mycounts = mystring.count('i')\n", "print(mystring)\n", "mycounts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Flow control\n", "\n", "Like any other programming language, Python posesses flow control mechanisms to repeat operations (for, while) or separate different cases (if, else). \n", "\n", "The main specificities of Python are:\n", "- **INDENTATION:** All the commands that belong to the same flow contol block have to be indentend at the same level. Let's create a simle for loop\n", "- flow controls start after ```:```\n", "- no parenthesis or special signs are used in the statement which reads almost like english" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "mylist = [1,2,3,4,5,6,7,8]" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "9\n", "16\n", "25\n", "36\n", "49\n", "64\n" ] } ], "source": [ "for x in mylist:\n", " y = x**2\n", " print(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we un-indent the print statement it doesn't belong to the loop anymore and is executed *after* it:" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "64\n" ] } ], "source": [ "for x in mylist:\n", " y = x**2\n", "print(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If statements follow the same rules:" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "no\n" ] } ], "source": [ "a = 3\n", "b = 5\n", "\n", "if a > b:\n", " print('yes')\n", "else:\n", " print('no')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variable scope\n", "\n", "The last point which brings sometimes confusion is the in what context variables are available or in other words their *scope*. Let's imagine the following code:" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "initial defintion: 1\n", "value in myfun: 2\n", "value in myfun2: 1\n", "final defintion: 1\n" ] } ], "source": [ "a = 1\n", "\n", "def myfun():\n", " a = 2\n", " print(f'value in myfun: {a}')\n", " \n", "def myfun2():\n", " print(f'value in myfun2: {a}')\n", " \n", "print(f'initial defintion: {a}')\n", "\n", "myfun()\n", "myfun2()\n", "print(f'final defintion: {a}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So first we define ```a = 1```. Then we define two functions. In one of them we redefine the same variable as ```a = 2``` and print it, in the second one, we use it without redefining it. From the print statements we see that:\n", "1. The variable ```a``` defined in the notebook (or script) is **unaffected** by what happens in functions.\n", "2. If the same variable is redefined within a function it is a **new and independent** variable.\n", "3. A variable defined in the notebook (or script) is **globally** available to the functions used there.\n", "\n", "Finally, let's see what happens at the level of modules. In the course_functions.py module, we defined the same functions as ```myfun``` and ```myfun2```. We load them first and then call them:" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "from course_functions import myfun_in_script, myfun_in_script2" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "final defintion: 2\n" ] } ], "source": [ "myfun_in_script()" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'a' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmyfun_in_script2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m~/OneDrive - Universitaet Bern/Courses/BiaPy/BIAPy/course_functions.py\u001b[0m in \u001b[0;36mmyfun_in_script2\u001b[0;34m()\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 46\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmyfun_in_script2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 47\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf'final defintion: {a}'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'a' is not defined" ] } ], "source": [ "myfun_in_script2()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see now that even though we defined the variable ```a``` in this notebook, the function ```myfun_in_script2``` in the module is **not aware of it**. Thus ```a``` is only global at the notebook level. \n", "\n", "There are other potential cases not mentioned here. For a more in-depth look at this topic you can read more in this great [notebook](https://nbviewer.jupyter.org/github/rasbt/python_reference/blob/master/tutorials/scope_resolution_legb_rule.ipynb) from Sebastian Raschka (you can also check out his great [book on Machine Learning](https://github.com/rasbt/python-machine-learning-book-3rd-edition)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 4 }