INTRO2CS Tirgul 12 – Networks and GUI. Today: Network and Communication GUI - tkinter.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
-
Upload
augustine-jefferson -
Category
Documents
-
view
225 -
download
0
description
Transcript of Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
![Page 1: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/1.jpg)
1
Scope, Aliasing, Tuples & Mutability
Intro2CS – week 4a
![Page 2: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/2.jpg)
2
Scope (quick reminder)
![Page 3: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/3.jpg)
3
Scope
• Variables inside a function are local. They are not recognized or known outside of it.– Hides internal structure– Makes function more independent of code outside
def a(x):y = x + 2return y
a(1)print(x)print(y)
Error!Error!
External scope:
Function a():x,y
![Page 4: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/4.jpg)
Scope
On the other hand, a function knows of variables that are outside.
4
y=2def a():
print(y)y=3a() Will work, and print the value of y (that is 3)
External scope:
y
Function a():
Very confusing. Try to avoid it (except with constants). Instead, give needed values as parameters!
![Page 5: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/5.jpg)
5
Scope
• But, functions do not usually change variables outside. This can be very bad for code readability:
a=1b=3c=4
c = func(b)Did any of my variables change? Which ones?
We ASSUME that only c changes
![Page 6: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/6.jpg)
6
Scope
• Python assumes that if you are assigning a value to a variable inside a function, you mean a new local variable (that happens to have the same name): x=1
y=2def f(x): x=x+1 y=x+1
f(1)print(x)print(y)
External scope:
x,y
Function a(x):
x,y
External variables unchanged. (Prints 1,2)
![Page 7: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/7.jpg)
7
Side effects and global variables
• If you insist, you can indeed reach out and touch an external “global” variable:
x=1def a(): global x x=x+1a()a()print(x)
Will print 3
This is very very bad code! Avoid this!
![Page 8: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/8.jpg)
8
Scope
• Scope can be more complex, and have several nested levels:
def f1(x):def f2(y):
return y**2 return f2(f2(x))
def f3(x):print(x)
External scope:
Function f1(x):
Function f3(x):
Function f2(y):
![Page 9: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/9.jpg)
9
References, Aliases and Copies
![Page 10: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/10.jpg)
10
References• It is important to understand how information is
represented internally. • Variables do not directly hold a value, but
instead hold its address in memory. – a “reference” to the value / object.
x = 3y = “hello”
x: 27191
y: 22130
“Hello”
Cell number 22130
3
Cell number 27191
![Page 11: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/11.jpg)
11
ReferencesHow we often think about this:Variables point to the value.
(We usually don’t care about the address in memory)
x = “hello”y = 3
x:
y:
“Hello”
3
![Page 12: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/12.jpg)
12
AssignmentAssignment copies the address from one variable to the other.
x = “hello”y = x
print(id(x))Print(id(y))
x: 22130
y:
“Hello”
22130
Builtin function id will show you the number of the memory cell (in most python implementations)
![Page 13: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/13.jpg)
13
ListsLists (and other containers) also work the same way. They reference the values / objects inside them.
x = [1,”hello”,2.0]
x: 22130 [2275,2344,24551]
1
“Hello”
2.0
![Page 14: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/14.jpg)
14
ListsImplications:
x = [1,”hello”,2.0]y = x
y[0] = 53print(x[0])#output will be 53
x: 22130
y:
[2275,2344,24551]
221301
“Hello”
2.0
53
2288
![Page 15: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/15.jpg)
15
Copying the listIn contrast to the previous examples…
x = [1,”hello”,2.0]y = x[:]
y[0] = 53print(x[0])#output will be 1
x: 22130
y: 42445
[2275,2344,24551]
1 “Hello”2.0
53
Slicing creates a brand new list!
A “shallow copy”
[ , , ][2275,2344,24551]2288
![Page 16: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/16.jpg)
16
Comparing two listsTwo questions we might ask:1. Are the two lists actually the same list?
(the is operator)2. Are the contents of the two lists the same?
(the == operator)
x = [1,”hello”,2.0]y = x[:]z = x
print(x is y) #Falseprint(x == y) #Trueprint(z is x) #True
![Page 17: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/17.jpg)
17
append() vs. +
• The + operator on lists creates a new list
• append() and extend() do not
x = [1,2,3]y = x + [4,5,6]
print(x is y) #False
x = [1,2,3]x.extend([4,5,6])x.append(7)
![Page 18: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/18.jpg)
18
How else can we copy lists?
x = [1,2,3]y = copy.copy(x)z = list(x)
print(y is x) #Falseprint(z is x) #False
![Page 19: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/19.jpg)
19
Tuples• Tuples are another sequence datatype• Just like lists• Except they are immutable. cannot be changed.
x = (0, 1, 2)x = 0, 1, 2 #equivalentu = (3,) #a tuple of size 1
print(len(x)) #lengthy = tuple(range(3)) #constructorz = x + y #z is a new tupleprint(z[2:4]) #slicing works
for item in x: print(item) #works too
x[0] = 3 #ERROR!x.append(2) #ERROR!
![Page 20: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/20.jpg)
20
Unpacking
Allows assignments to multiple variables
coord = (1, 2, 3)x, y, z = coord
print(x)
def func(): return (1,2)
x, y = func()
So actually… functions have a single return value
This is tuple unpacking.
![Page 21: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/21.jpg)
21
Lists and FunctionsFunctions can accept lists as parameters:
def set_zero(lst, i): lst[i] = 0
my_list = [1,2,3]set_zero(my_list,1)print(my_list)
my_list:
lst:
[2275,2544,3551]
1
2
3
0
2278 Result: Function alters list, in outer scope, but without having to return it.
Must be aware of this when we program!
![Page 22: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/22.jpg)
22
Mutable and Immutable Types
• We can pass immutable types to a function knowing that they will not be changed.
def funky_function(tpl): … some code …
my_tpl = 1,2,3funky_function(my_tpl)print(my_tpl)
![Page 23: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/23.jpg)
23
• Strings are immutable too.
• So are ints, floats, booleans, ranges
x = “hi”y = “bye”
z = x + y #z is a new string!
w = z[2:4] #slicing doesn’t change#original strings…
x[0] = 3 #ERROR!x.append(“a”) #ERROR!
![Page 24: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/24.jpg)
24
Immutable types are often reused
(and sometimes not)
![Page 25: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/25.jpg)
25
How to deep copy lists
original
original = [["Moe", 100], ["Joe",30]]alias = original
original[1][1] = "CHANGED"original.append([“Fred",95])
print(alias)
“Moe”
100
“Joe”
30
“Fred”
95
alias
“CHANGED”
![Page 26: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/26.jpg)
26
How to deep copy lists
original
“Moe”
100
“Joe”
30
shallow_cpy
original = [["Moe", 100], ["Joe",30]]alias = originalshallow_cpy = original[:]
original[1][1] = "CHANGED"original.append([“Fred",95])
print(alias) ; print(shallow_cpy)
“Fred”
95
“CHANGED”
![Page 27: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/27.jpg)
27
How to deep copy lists
original
“Moe” “Joe”
original = [["Moe", 100], ["Joe",30]]alias = originalshallow_cpy = original[:]deep_cpy = copy.deepcopy(original)
original[1][1] = "CHANGED"original.append([“Fred",95])
print(alias) ; print(shallow_cpy) ; print(deep_cpy)
deep_cpy
100 30
“Fred”
95
“CHANGED”
![Page 28: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/28.jpg)
28
How to deep compare
== does deep comparison. (It uses == to compare all cells of a list)
original = [["Moe", 100], ["Joe",30]]deep_cpy = copy.deepcopy(original)
print(deep_cpy is original) #Falseprint(deep_cpy[0] is original[0]) #Falseprint(deep_cpy == original) #True
![Page 29: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/29.jpg)
29
Mind benders
a is a tuple. It is immutable.
is this going to run?
Did we change the contents of the tuple?
a = ([1,2,3], “hi”, 2.0)a[0][0] = 52
![Page 30: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/30.jpg)
30
Mind benders
• What does the following code do?
• Can a list contain itself?
a = [1,2]b = [1,2]a[0] = bb[0] = a
print(a[0])
![Page 31: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/31.jpg)
31
Programs with multiple files
![Page 32: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/32.jpg)
32
Importing filesSuppose foo.py has code we want to use in bar.pyIn bar.py we can write:
Here’s more info on handling cyclic imports. https://docs.python.org/3/faq/programming.html#how-can-i-have-modules-that-mutually-import-each-other
import foofoo.func1()
from foo import func1,func2func1()
from foo import *func1()
preferred alternatives. No problems with name collisionimport foo as my_foo
my_foo.func1()
![Page 33: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/33.jpg)
33
Importing runs the code
import foo
foo.func1()…more_stuff…
def func1():dostuff…
print(“hello”)
Run this. Okay.If we run this, also prints.
import foo
foo.func1()…more_stuff…
def func1():dostuff…
if __name__ == “__main__”: print(“hello”)
Run this. Okay.Run this. Okay.
foo.py
foo.py
bar.py
bar.py
__name__ will have the value “__main__” only if the current module is first to execute
We want to be able to run foo.py, but also want to use its code in bar.py
![Page 34: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/34.jpg)
34
Function Extras
![Page 35: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/35.jpg)
35
Default values for arguments
def p_print(text, times=1, p_char="*" ): print(p_char * (len(text)+4)) for i in range(times): print(p_char, text, p_char) print(p_char * (len(text)+4))
p_print("hello")p_print("hello",2)p_print("hello",2,'@')p_print("hello",p_char = '@')p_print(p_char = '@', text = "hello", times=2)p_print(p_char = '@',"hello") #ERROR!
![Page 36: Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.](https://reader035.fdocuments.in/reader035/viewer/2022062311/5a4d1b2e7f8b9ab05999a166/html5/thumbnails/36.jpg)
36
Unknown number of arguments
• You can define functions that accept as many arguments as the user wishes to provide.
• Arguments are placed in a tuple.
def is_word_in(word, *text_list): for text in text_list: if word in text: print(word, "is in" ,text) is_word_in("run", "rune", "cat",
"prune", "apple" ,"runner")is_word_in("run")