Unit F Reading assignment

Examples

argument packing.png

argument dictionary packing

exceptions.png

exceptions2.png

function objects

functions in list

lambda

namespace

None

raise

sort key lambda

sort_itemgetter

Chapter 9 Functions

Functions

The first edition of the book fails to discuss the main function, a function that is very commonly used, and one that we will be using.
The short article Python main function discusses Python's main function and illustrates the way in which it is commonly invoked, as also shown below:
def main(): #call your functions from here. if __name__ == '__main__': main()

The first edition of the book says: When you call a function with arguments, the values of those arguments are copied to their corresponding parameters inside the function.
That is exactly how C works, but NOT exactly how Python works. It is very IMPORTANT to understand exactly how Python works.
In Python, the argument has a reference to an object. This reference is copied to the parameter, so the parameter in the function refers to the same object that the argument refers to.
This is called a shallow copy, when there are two references to the same object.

Define a Functions with def

Call a Function with Parentheses

Arguments and Parameters

None is Useful

Positional Arguments

Keyword Arguments

Specify Default Parameter Values

The rule given in the note in the book, that you should only use immutable objects for parameter default values, is good. I do not understand all the things that can happen if you use a mutable object as a parameter default object.

Explode/Gather Positional Arguments with *

Some helpful suplemental reading can be found at arbitrary number of parameters (scroll down to the section called Arbitrary Number of Parameters)

Explode/Gather Keyword Arguments with **

Keyword-Only Arguments

Mutable and Imutable Arguments

very importment

Docstrings

Functions are First-Class Citizens

scan

Inner Functions

omit

Closures

omit

Anonymous Functions: lambda

Read

Generators

Generator Functions

Read

Generator Comprehensions

omit

Decorators

omit

Namespaces and Scope

The examples in this section are a little confusing. If you do not do anything to change the way it works, variables inside the function are in a different namespace than variables that are not within that function.

Uses of _ and __ in Names

Recursion

Async Functions

omit

Exceptions

Handle Errors with try and except

Make Your Own Exceptions

This is a good first example of a Python class.
The UppercaseException class is a child of the Exception class.

Comming Up

Things to Do

omit