From fe42b933280691390a2a4741b7bcbccaa86a5c2b Mon Sep 17 00:00:00 2001 From: John Stachurski Date: Sun, 3 Mar 2024 19:47:07 +1100 Subject: [PATCH] misc --- lectures/names.md | 22 +++++++++++----------- lectures/oop_intro.md | 20 +++++++++++--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/lectures/names.md b/lectures/names.md index dbecf57f..3ce66b14 100644 --- a/lectures/names.md +++ b/lectures/names.md @@ -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. @@ -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 @@ -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 @@ -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 @@ -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 ``` diff --git a/lectures/oop_intro.md b/lectures/oop_intro.md index 730e1df8..708f1ee9 100644 --- a/lectures/oop_intro.md +++ b/lectures/oop_intro.md @@ -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. @@ -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 `. It's quite common for users to add methods to their that measure the length of the object, suitably defined. @@ -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__()`. @@ -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