Functions as arguments:
def func_a(a): print 'inside func_a' def func_b(y): print 'inside func_b' return y def func_c(z): print 'inside func_c' return z() print func_a() # func_a takes no parameters print 5 + func_b(2) # func_b takes one parameter print func_c(func_a) # func_c takes one parameter, another function
Show global scope and func_a scope while func_a is executing. Notice that it returns None, which gets printed. Similarly, func_b returns 2 and so 7 is printed.
Show three scopes: global, func_c, and func_a.
Scope example
Examples:
x objects
def f(y): x = 1 #x is re-defined in scope of f (different object) x += 1 print(x) x = 5 #x is defined outside the scope of f f(x) print(x) #prints the outer x object
x inside g is picked up from scope that called function g.
def g(y): print(x) #x from outside g print(x+1) #x from outside g x = 5 g(x) print(x) #prints the outer (and only) x object
x inside g is picked up from scope that called function g.
x is used with a read/write operator (where the read happens first) in a scope, even if it was defined in an outer scope.
def h(y): x += 1 x = 5 h(x) print(x) #does not reach hereThis yields
UnboundLocalError: local variable 'x' referenced before assignment. Would have been ok if we had done x = x + 1 instead of x += 1 --- the former reads the outer x and defines an inner x; the latter tries to read the inner x before it has been defined!
Complex example:
def g(x):
x = x + 1
print('g: y = ', y, 'x = ', x) # this causes an error, as y is neither defined in the current scope nor defined in the (static) outer scope
return x
def h(w):
y = 'abc'
return g(w)
x = 3
z = h(x)
More complex example:
def g(x):
def h():
x = 'abc'
x = x + 1
print('g: x = ', x)
h()
return x
x = 3
z = g(x)
Global scope has g -> some code and x -> 3. The
g scope has x -> 4 (reads from the outer object and defines an inner object) and h -> some code. This behaviour of reading from the outer scope and defining a new variable in the inner scope, is no longer possible in recent versions of Python (to avoid programming errors). The h scope has x -> 'abc' and returns None. On the return path, g returns 4 (for x) and in the global scope, this value is assigned to z.
Decomposition and abstraction are powerful together:
int, float, bool, string. Today, we will introduce compound data types:
te = () #empty tuple
t = (2, "hello", 3)
t[0] #evaluates to 2
(2, "hello", 3) + (4, 5) # evaluates to (2, "hello", 3, 4, 5)
t[1:2] #slice tuple, evaluates to ("hello",); notice the extra comma which indicates a tuple with one element
t[1:3] #slice tuple, evaluates to ("hello", 3)
len(t) #evaluates to 3
t[1] = 4 #gives error, cannot modify object
When a tuple is used on the left-hand side (LHS) of an assignment, then it
can be used to assign to multiple variables simultaneously. For example, this
can be conveniently used to swap variable values.
First try:
x = y y = x #This is an incorrect way to swap two variables
temp = x x = y y = temp #Correct way to swap two variables, but this can be written in a shorter way
(x, y) = (y, x)
A tuple can be used to return more than one value from a function
def quotient_and_remainder(x, y): q = x // y #integer division r = x % y return (q, r) (quot, rem) = quotient_and_remainder(43, 5)