try1: what does the following code print, if placed in a file and executed?
def f(n):
return 3 * n
def g():
return f
value = g()
print value(5)
try2: what does the following code print, if placed in a file and executed?
def g(n):
def f():
return 8*n
return f
value = g(5)
print value()
try3: what does the following code print, if placed in a file and executed?
class Klassy(object):
def __init__(self, n):
self.n = n
def val(self):
return 4*self.n
k = Klassy(5)
print k.val()
try4: what does the following code print, if placed in a file and executed?
class Klassy(object):
def __init__(self, n):
self.n = n
def val(self):
def g(m):
return 3 * self.n + m
return g
k = Klassy(4)
fn = k.val()
print fn(3)
try5: what does the following code print, if placed in a file and executed?
class Klassy(object):
def __init__(self, n):
self.n = n
def val(self):
def g(m):
return 3 * self.n + m
return g
k = Klassy(4)
fn = k.val()
k.n = 8
print fn(4)
try6: what does the following code print, if placed in a file and executed?
class Klassy(object):
def __init__(self, n):
self.n = n
def __call__(self):
return 5*self.n
k = Klassy(5)
print k()
try7: what does the following code print, if placed in a file and executed?
def some_function(other_fn, value):
value = value*5
value2 = other_fn(value)
return value2
def f(n):
return n + 1
def g(m):
return m - 1
print some_function(g, 5)
print some_function(f, 4)
try8: what does the following code print, if placed in a file and executed?
global_value = 6
def some_function(other_fn, value):
value = value*global_value
value2 = other_fn(value)
return value2
def f(n):
return n + 1
def g(m):
return m - 1
print some_function(g, 5)
print some_function(f, 4)
global_value = 2
print some_function(g, 5)
print some_function(f, 4)
This file can be edited directly through the Web. Anyone can update and fix errors in this document with few clicks -- no downloads needed.
For an introduction to the documentation format please see the reST primer.