Defining and Calling Functions
A function packages a piece of work behind a name. def creates it, the call runs it, and return decides what comes back - which is None if you never s...
A complete core Python path: syntax, data types, operators, strings, control flow, collections, functions, exceptions, files, modules, the standard library, OOP, iterators, generators, decorators, internals, concurrency, regular expressions, testing, typing, performance, security, problem solving, projects, interview preparation and exams.
110 notes in 33 topics
A function packages a piece of work behind a name. def creates it, the call runs it, and return decides what comes back - which is None if you never s...
Positional, keyword, default, variable length and keyword only arguments - five styles, one fixed order, and one famous trap involving mutable default...
Python resolves a name by looking in four places in a fixed order: local, enclosing, global, built-in. Assignment anywhere in a function makes the nam...
A lambda is a small anonymous function written as a single expression. It exists for the places where naming a function would add nothing.
Functions in Python are ordinary objects. They can be stored, passed, returned and built at runtime, and that fact underlies sorting keys, decorators...
A closure is an inner function that keeps hold of variables from the function that created it, long after that function has returned.
A recursive function calls itself on a smaller version of the problem until it reaches a case it can answer directly. Every one needs a base case and...
Fibonacci shows why naive recursion can be catastrophically slow, memoisation fixes it in one line, and some problems are worth the stack frames while...
Three kinds of error, found at three different moments. Only one of them can be caught, and the one that catches nobody out is the most dangerous.
try runs risky code, except handles a failure, else runs when nothing failed, and finally always runs. Each block has one job and using the wrong one...
Every exception is a class in one inheritance tree. Catching a parent catches all its children, which is what makes handler order and handler width ma...
raise signals a failure your own code has detected. A custom exception class gives that failure a name callers can catch precisely.