Day 9: Tuesday, Feb 4th, 2014

  1. Read the following two links: what is WSGI, Wikipedia on WSGI; skim a stack overflow question and the WSGI PEP
  2. Quiz: answer some questions
  3. What is StackOverflow anyway? Hey, and what’s a PEP?
  4. WSGI in practice; separation of concerns. (Presentation.)
  5. Thinkin’ ‘bout functions.

Fun with functions and callables

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)

Table Of Contents

Previous topic

Day 10: Thursday, Feb 6th, 2014

Next topic

Homework 4

This Page

Edit this document!

This file can be edited directly through the Web. Anyone can update and fix errors in this document with few clicks -- no downloads needed.

  1. Go to Day 9: Tuesday, Feb 4th, 2014 on GitHub.
  2. Edit files using GitHub's text editor in your web browser (see the 'Edit' tab on the top right of the file)
  3. Fill in the Commit message text box at the bottom of the page describing why you made the changes. Press the Propose file change button next to it when done.
  4. Then click Send a pull request.
  5. Your changes are now queued for review under the project's Pull requests tab on GitHub!

For an introduction to the documentation format please see the reST primer.