<commit_before>---
layout: "post"
title: "Scope"
date: "2016-04-19 21:57"
---

## ⏳ Do Now:

Rank the following from easiest to hardest:

1. Importing built-in functions
2. Using `randint`
3. Abstraction/creating functions
4. Passing int/str/float/bool arguments into functions
5. Calling a function
6. List syntax
7. Return vs print

We will have a quiz next class covering all of the above topics. Is there any topic you would like to focus on and cover more of? <br><br>

Type the following into the terminal and run the program:

```python
my_list = ['a', 'b', 'c', 'd']
# input: a list of strings
# output: None
def my_function(list_argument):
	list_argument[0] = 'z'
print(my_list)
my_function(my_list)
print(my_list)
```
What happened? Try writing a similar program but passing in integers instead of a list. What happens?

---

## 📖 Reading

### Variables and parameters are local

When you create a variable inside a function, it is local, which means that it only exists inside the function. For example:

```python
def together(part1, part2):
    cat = part1 + part2
    print(cat)
```

This function takes two arguments, adds them, and prints the result twice. Here is an example that uses it:

```python
line1 = 'Hello there, '
line2 = 'children.'
# What prints?
```
When cat_twice terminates, the variable cat is destroyed. If we try to print(it, we get an exception:)

```python
>>> print(cat)
NameError: name 'cat' is not defined
```
Parameters are also local. For example, outside `together`, there is no such thing as `part1` or `part2`.

### Aliasing

If `a` refers to an object and you assign `b = a`, then both variables refer to the same object:

```python
a = [1, 2, 3]
b = a
b is a
True
```

The state diagram looks like:

![State Diagram](http://www.greenteapress.com/thinkpython/html/thinkpython016.png)

The association of a variable with an object is called a reference. In this example, there are two references to the same object.
An object with more than one reference has more than one name, so we say that the object is aliased.

If the aliased object is mutable, changes made with one alias affect the other:

```python
b[0] = 17
print(a)
[17, 2, 3]
```

Although this behavior can be useful, it is error-prone. In general, it is safer to avoid aliasing when you are working with mutable objects.

---

## <span class="mega-octicon octicon-mark-github"></span> Lab

<kbd>In PyCharm, create a new file called <mark>Scope_Lab.py</mark></kbd>

### Aliasing

Will updating b affect a? Explain why or why not?

```python
a = [1, 2, 4]
b = a
```

Predict what `my_list` list will print out when this code is run. If you are not sure check the code by copying and running it.

```python
# input: a list of ints
# output: an int
def update_list(a_list):
    a_list[3] = "yo"
    b = a_list[4]
    b = 100
my_list = [1, 2, 3, 4, 5]
update_list(my_list)
```

### Scope

Draw a stack diagram for the following:
<commit_msg>stuff<commit_after>---
layout: "post"
title: "Scope"
date: "2016-04-19 21:57"
---

## ⏳ Do Now:

Rank the following from easiest to hardest:

1. Importing built-in functions
2. Using `randint`
3. Abstraction/creating functions
4. Passing int/str/float/bool parameters into functions
5. Calling a function
6. List syntax
7. Return vs print

We will have a quiz next class covering all of the above topics. Is there any topic you would like to focus on and cover more of? <br><br>

Type the following into the terminal and run the program:

```python
my_list = ['a', 'b', 'c', 'd']
# input: a list of strings
# output: None
def my_function(list_argument):
	list_argument[0] = 'z'
print(my_list)
my_function(my_list)
print(my_list)
```
What happened? Try writing a similar program but passing in integers instead of a list. What happens?

---

## 📖 Reading

### Variables and parameters are local

When you create a variable inside a function, it is local, which means that it only exists inside the function. For example:

```python
def together(part1, part2):
    cat = part1 + part2
    print(cat)
```

This function takes two parameters[^parameters] , adds them, and prints the result twice. Here is an example that uses it:

[^parameters]: `part1` and `part2`

```python
line1 = 'Hello there, '
line2 = 'children.'
together(line1, line2)
# What prints?
```
When `together` terminates, the variable cat is destroyed. What happens if we try to print it?

```python
>>> print(cat)
NameError: name 'cat' is not defined
```
Parameters are also local. For example, outside `together`, there is no such thing as `part1` or `part2`.

### Aliasing

If `a` refers to an object and you assign `b = a`, then both variables refer to the same object:

```python
a = [1, 2, 3]
b = a
b is a
True
```

### Local, Global, Build-in `LBG`

Python understands names at three **scopes**: `l`ocal, then `g`lobal, then `b`uilt-in.

---

## 🖥 Lab

<kbd>In PyCharm, create a new file called <mark>Scope_Lab.py</mark></kbd>

### Aliasing

Will updating b affect a? Explain why or why not?

```python
a = [1, 2, 4]
b = a
```

Predict what `my_list` list will print out when this code is run. If you are not sure check the code by copying and running it.

```python
# input: a list of ints
# output: an int
def update_list(a_list):
    a_list[3] = "yo"
    b = a_list[4]
    b = 100
my_list = [1, 2, 3, 4, 5]
update_list(my_list)
```

### Scope

Draw a stack diagram for the following:

{'fraction_non_alphanumeric': 0.11285846438482887, 'fraction_numerical': 0.01887141535615171, 'mean_word_length': 3.96875, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6488112', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1742, 'n_words': 801}