GitHub

@@ -670,36 +670,28 @@ First,

670670

* The global namespace `{}` is created.

671671672672

```{figure} /_static/lecture_specific/oop_intro/global.png

673-

:figclass: auto

674673

```

675674676675

* The function object is created, and `g` is bound to it within the global namespace.

677676

* The name `a` is bound to `0`, again in the global namespace.

678677679678

```{figure} /_static/lecture_specific/oop_intro/global2.png

680-

:figclass: auto

681679

```

682680683681

Next `g` is called via `y = g(10)`, leading to the following sequence of actions

684682685683

* The local namespace for the function is created.

686684

* Local names `x` and `a` are bound, so that the local namespace becomes `{'x': 10, 'a': 1}`.

685+

* Note that the global `a` was not affected by the local `a`.

687686688-

* Note that the global `a` was not affected by the local `a`.

689687

```{figure} /_static/lecture_specific/oop_intro/local1.png

690-

:figclass: auto

691688

```

692689693-694-695690

* Statement `x = x + a` uses the local `a` and local `x` to compute `x + a`, and binds local name `x` to the result.

696-697-698691

* This value is returned, and `y` is bound to it in the global namespace.

699692

* Local `x` and `a` are discarded (and the local namespace is deallocated).

700693701694

```{figure} /_static/lecture_specific/oop_intro/local_return.png

702-

:figclass: auto

703695

```

704696705697

@@ -747,21 +739,18 @@ Here's what happens

747739

* `f` is registered as a function in the global namespace

748740749741

```{figure} /_static/lecture_specific/oop_intro/mutable1.png

750-

:figclass: auto

751742

```

752743753744

* `x` bound to `[1]` in the global namespace

754745755746

```{figure} /_static/lecture_specific/oop_intro/mutable2.png

756-

:figclass: auto

757747

```

758748759749

* The call `f(x)`

760750

* Creates a local namespace

761751

* Adds `x` to the local namespace, bound to `[1]`

762752763753

```{figure} /_static/lecture_specific/oop_intro/mutable3.png

764-

:figclass: auto

765754

```

766755767756

```{note}

@@ -786,12 +775,10 @@ print(f(x), x)

786775

* Returns the list `[2]`

787776788777

```{figure} /_static/lecture_specific/oop_intro/mutable4.png

789-

:figclass: auto

790778

```

791779

* The local namespace is deallocated, and the local `x` is lost

792780793781

```{figure} /_static/lecture_specific/oop_intro/mutable5.png

794-

:figclass: auto

795782

```

796783797784

If you want to modify the local `x` and the global `x` separately, you can create a [*copy*](https://docs.python.org/3/library/copy.html) of the list and assign the copy to the local `x`.

@@ -803,9 +790,9 @@ We will leave this for you to explore.

803790804791

Messages in this lecture are clear:

805792806-

* In Python, *everything in memory is treated as an object*.

807-

* Zero, one or many names can be bound to a given object.

808-

* Every name resides within a scope defined by its namespace.

793+

* In Python, *everything in memory is treated as an object*.

794+

* Zero, one or many names can be bound to a given object.

795+

* Every name resides within a scope defined by its namespace.

809796810797

This includes not just lists, strings, etc., but also less obvious things, such as

811798

@@ -908,7 +895,7 @@ boolean object `True`.

908895

```{hint}

909896

:class: dropdown

910897911-

You can use `callable()` to test whether an attribute of an object can be called as a function

898+

You can use `callable()` to test whether an attribute of an object can be called as a function

912899

```

913900914901

```{exercise-end}

Read the original on github.com ↗