Skip to content

Week 2 Notes

  • Reading Ch. 1.3: Defining New Functions (08/05/22)
    • Function signatures: a description of the formal parameters of a function
    • Intrinsic name: the name appearing in the function, unique to each function
    • Bound name: the name in a frame
    • The scope of a local name is limited to the body of the function
    • (Non-rebellious) conventions for names
      • Functions: lowercase, seperated by underscores
      • Parameters: lowercase, seperated by underscores, and evoke roles
    • Functional abstraction: a function definition should suppress details
      • Domain: set of arguments
      • Range: set of return values
      • Intent: the relationship the function computes between inputs and outputs and side effects
  • Reading Ch. 1.4: Designing Functions (08/05)
    • Each function should have exactly one job, can be described concisely, and defined generally
    • Don’t repeat yourself principle
    • Default argument values: optional arguments whole default values are bound to the parameter name when not provided
  • Reading Ch. 1.5: Control (08/05)
    • Statements can have expressions as components (e.g: def, return)
    • Expressions can be executed like statements, but their evaluated values are not used
    • Clause: a header and a suite of following statements
    • Compound statement: one or more cla uses
    • Conditional statements:
      • Evaluate the header’s expression
      • If it’s true, execute the suite. Then skip all subsequent clauses
    • Boolean operators: and, or, not
      • Evaluate, evaluate, evaluate!
    • Testing: validating sample calls with specific argument values
      • Assertions: Tests are typically written in the same file or a neighboring file with the suffix _test.py, rather than into the interpreter
        • assert f(x) == y, ‘This text will be displayed if the expression evaluates to false
      • Doctests: include a sample interactive session that calls the function and displays the expected return value

doctest interaction

  • Effective testing is to write and run tests after implementing new functions, or even before!
  • Unit test: a test that applies a single function
  • Good program design comes with exhaustive unit testing (all-cases of every single function)
  • Lecture 3: Control (09/05/22)
  • Lab 01: Variables & Functions, Control (09/05/22)
  • Reading Ch 1.6: Higher-Order Functions (10-11, 19/05/22)
    • Higher-order functions: can accept functions as arguments or return functions as values
    • Higher-order functions are a more powerful means of abstraction than user-defined functions, expressing general methods of computation, regardless of the particular functions they call
    • By mapping each general concept or equation with its own short function, the global becomes cluttered with unique function names, and we are constrained by particular function signatures. Nested function definitions address both.
    • Lexical scope: names are shared among nested definitions
    • Function parent: the first frame of the environment in which the function was defined. When that function is called, the frame created has the same parent
    • Extended environments: by calling nested functions, we can create longer chain of frames
    • Lexical scoping can reduce global function names and help nested functions share an environment
    • Locally defined functions maintain their parent environments when they are returned
    • Currying: converting a function that takes multiple arguments into a chain of functions that each take a single argument using higher-order functions
    • Lambda expressions: evaluates to a function that has a single return expression. Assignment and control statements are not allowed
    • Functions in Python are granted “first-class” status:
      • They may be bound to names
      • They may be passed as arguments to functions
      • They may be returned as results of functions
      • They may be included in data structures
    • Decorators: used for tracing, affects def statement and the function attached by extending its behavior without explicitly changing it
  • Lecture 4: Higher-Order Functions (11/05/22)
  • Project 1: The Game of Hog (11-15/05/22)
    • *args syntax: used to pass an arbitrary amount of arguments into a function
  • Lecture 5: Environments (20/05/22)
  • Homework 02: (20-21, 24/05/22)
    • It is advantageous to consider input as both function and value
  • Discussion 01: Control, Environment Diagrams (15/07/22)