Standard library essentials

Standard library essentials

Questions and materials on "Standard library essentials"

10 audio · 2:27

Nortren·

What does the collections module provide?

0:17
Collections provides specialized container types beyond the built-ins. The most useful are deque for fast append and pop from both ends, Counter for counting hashable items, defaultdict for dictionaries with default values for missing keys, OrderedDict for explicit ordering control, and ChainMap for combining multiple mappings.

What is collections.Counter?

0:15
Counter is a dict subclass for counting hashable objects. It takes an iterable and produces a mapping from elements to their counts. It supports arithmetic operations between counters and methods like most_common to return the top elements. It is the idiomatic way to count occurrences.

What is collections.defaultdict?

0:13
Defaultdict is a dict subclass that calls a factory function to provide default values for missing keys. For example, defaultdict of list returns an empty list for any new key on access. This eliminates the boilerplate of checking for key existence before appending.

What is collections.deque?

0:13
Deque is a double-ended queue with constant-time appends and pops from both ends, unlike a list which is linear time for left operations. It is the right choice for queues and stacks. It also supports fast rotation and bounded length via maxlen.

What does itertools provide?

0:16
Itertools provides building blocks for iterators. Examples include count for infinite counters, cycle for infinite repetition, chain for concatenation, islice for slicing iterators, takewhile and dropwhile for conditional iteration, combinations and permutations for combinatorial generation, and product for Cartesian products.

What does functools provide?

0:15
Functools provides higher-order functions and tools for working with functions. Key tools include partial for partial application, reduce for reduction, lru_cache and cache for memoization, wraps for preserving function metadata in decorators, and singledispatch for function overloading by argument type.

What is functools.partial?

0:12
Partial returns a new callable with some arguments of the original function fixed. It is used to specialize a general function into a more specific one without writing a wrapper. It is useful when passing functions as callbacks or to map and filter.

What is functools.singledispatch?

0:13
Singledispatch is a decorator that turns a function into a generic function whose behavior depends on the type of the first argument. You register implementations for specific types using the register method. It is Python's mechanism for single-argument method overloading.

What does the pathlib module do?

0:16
Pathlib provides an object-oriented interface for filesystem paths. The Path class supports cross-platform path manipulation, file reading and writing, directory traversal, and globbing. It replaces older approaches based on os.path strings and is the recommended modern way to handle paths.

What is the datetime module?

0:17
Datetime provides classes for working with dates and times: date, time, datetime, timedelta, and timezone. It supports arithmetic, formatting with strftime, parsing with strptime, and timezone-aware operations. The zoneinfo module added in Python 3.9 provides IANA timezone support. ---