Functions and arguments

Functions and arguments

Explore the power of functions and object-oriented programming in Python. This section delves into function definitions, class structures, and advanced topics such as properties and descriptors, ensuring you understand how to create robust and reusable code.

11 audio · 2:50

Nortren·

What is the difference between positional and keyword arguments?

0:14
Positional arguments are matched to parameters by their position in the call. Keyword arguments are matched by name using the equals sign. Positional must come before keyword in a call. Functions can be defined to accept either, both, or to require one specific kind.

What are star args and double star kwargs?

0:14
Star args collects extra positional arguments into a tuple. Double star kwargs collects extra keyword arguments into a dictionary. They allow functions to accept a variable number of arguments. The names args and kwargs are conventional but any name works.

What are positional-only and keyword-only parameters?

0:17
Positional-only parameters cannot be passed by name. They are declared by placing a forward slash in the parameter list; everything before the slash is positional-only. Keyword-only parameters must be passed by name. They are declared after a star in the parameter list. Both features improve API clarity.

What is unpacking in function calls?

0:16
Unpacking lets you pass an iterable as positional arguments using a single star, or a dictionary as keyword arguments using double star. For example, calling f with star my list passes the elements of my list as separate positional arguments. This is the inverse of args and kwargs collection in definitions.

What is a lambda function?

0:15
A lambda is an anonymous function defined with the lambda keyword. It can have any number of parameters but only a single expression as its body, and it returns the value of that expression. Lambdas are useful as small inline callables, especially as keys to sort or as arguments to map and filter.

What is the difference between a function and a method?

0:14
A function is a standalone callable defined with def at the module level. A method is a function defined inside a class. When called on an instance, the instance is automatically passed as the first argument, conventionally named self. Methods bound to an instance are called bound methods.

What is a closure in Python?

0:16
A closure is a function defined inside another function that captures variables from the enclosing scope. The inner function retains access to those variables even after the outer function has returned. Closures are commonly used to create functions with preconfigured behavior, such as decorators and factory functions.

What is the late binding closure pitfall?

0:16
When a closure captures a variable from an enclosing loop, it captures the variable name, not the value at the time of definition. By the time the closure is called, the variable holds the final value of the loop. The fix is to capture the value at definition time, often by using a default argument.

What is a decorator?

0:16
A decorator is a callable that takes a function and returns a new function, typically extending or modifying its behavior. Decorators are applied with the at-sign syntax above a function definition. They are widely used for logging, caching, access control, registration, and similar cross-cutting concerns.

How do you preserve function metadata when writing a decorator?

0:15
When a decorator wraps a function, the wrapped function's name, docstring, and other metadata are replaced by those of the wrapper. The functools.wraps decorator, applied to the inner wrapper function, copies the original metadata. This is important for debugging and introspection.

What is the difference between functools.lru_cache and functools.cache?

0:17
lru_cache is a decorator that memoizes function results using a least-recently-used cache with a configurable maximum size. functools.cache, added in Python 3.9, is equivalent to lru_cache with no size limit. Both are useful for expensive pure functions where the same arguments often produce the same result. ---