Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
jstac committed Mar 3, 2024
1 parent 5fe985b commit fe42b93
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
22 changes: 11 additions & 11 deletions lectures/names.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ After binding the name `g` to the same object, we can use it anywhere we would u

What happens when the number of names bound to an object goes to zero?

Here's an example of this situation, where the name `x` is first bound to one object and then rebound to another
Here's an example of this situation, where the name `x` is first bound to one object and then **rebound** to another

```{code-cell} python3
x = 'foo'
id(x)
x = 'bar'
id(x)
```

```{code-cell} python3
x = 'bar' # No names bound to the first object
```
In this case, after we rebind `x` to `'bar'`, no names bound are to the first object `'foo'`.

What happens here is that the first object is garbage collected.
This is a trigger for `'foo'` to be garbage collected.

In other words, the memory slot that stores that object is deallocated, and returned to the operating system.
In other words, the memory slot that stores that object is deallocated and returned to the operating system.

Garbage collection is actually an active research area in computer science.

Expand Down Expand Up @@ -151,7 +151,7 @@ mathfoo.pi

These two different bindings of `pi` exist in different namespaces, each one implemented as a dictionary.

We can look at the dictionary directly, using `module_name.__dict__`
If you wish, you can look at the dictionary directly, using `module_name.__dict__`.

```{code-cell} python3
import math
Expand All @@ -162,7 +162,7 @@ math.__dict__.items()
```{code-cell} python3
import mathfoo
mathfoo.__dict__.items()
mathfoo.__dict__
```

As you know, we access elements of the namespace using the dotted attribute notation
Expand All @@ -171,10 +171,10 @@ As you know, we access elements of the namespace using the dotted attribute nota
math.pi
```

In fact this is entirely equivalent to `math.__dict__['pi']`
This is entirely equivalent to `math.__dict__['pi']`

```{code-cell} python3
math.__dict__['pi'] == math.pi
math.__dict__['pi']
```

## Viewing Namespaces
Expand Down Expand Up @@ -524,7 +524,7 @@ Here's what happens
```{figure} /_static/lecture_specific/oop_intro/mutable1.png
```

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

```{figure} /_static/lecture_specific/oop_intro/mutable2.png
```
Expand Down
20 changes: 11 additions & 9 deletions lectures/oop_intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,11 @@ inspect(10, methods=True)

In fact there are still more methods, as you can see if you execute `inspect(10, all=True)`.



## A Little Mystery

In this lecture we claimed that Python is object oriented.
In this lecture we claimed that Python is, at heart, an object oriented language.

But here's an example that looks more procedural.

Expand All @@ -289,13 +291,12 @@ m = len(x)
m
```

If Python is object oriented, why don't we use `x.len()`? Isn't this
inconsistent?
If Python is object oriented, why don't we use `x.len()`?

The answers are related to the fact that Python aims for consistent style.
The answer is related to the fact that Python aims for readability and consistent style.

In Python, it is common for users to build custom objects --- we discuss how to
do this [later](python_oop)`.
do this {doc}`later <python_oop>`.

It's quite common for users to add methods to their that measure the length of
the object, suitably defined.
Expand All @@ -305,10 +306,10 @@ When naming such a method, natural choices are `len()` and `length()`.
If some users choose `len()` and others choose `length()`, then the style will
be inconsistent and harder to remember.

To avoid this, the creator of Python chose to have some built-in functions
like `len()`, to make clear that `len()` is the convention.
To avoid this, the creator of Python chose to add
`len()` as a built-in function, to help emphasize that `len()` is the convention.

Now, having said all of this, Python still is object oriented under the hood.
Now, having said all of this, Python *is* still object oriented under the hood.

In fact, the list `x` discussed above has a method called `__len__()`.

Expand Down Expand Up @@ -341,7 +342,8 @@ This includes not just lists, strings, etc., but also less obvious things, such
* files opened for reading or writing
* integers, etc.


Remember that everything is an object will help you interact with your programs
and write clear Pythonic code.

## Exercises

Expand Down

0 comments on commit fe42b93

Please sign in to comment.