Functions Flashcards

1
Q

Arguments

A

positional *args
keyword **kwargs
variable-length (varargs): *args, **kwargs
required
optional
actual parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Parameters

A

(pos, /, pos_or_kw, *, kw)
pos and **kwargs can have same names
PEP 570
formal parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Parameters order

A

non-default parameters, default parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Arguments order

A

positional, keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Lambda

A

bound variable
higher-order
single expression
no type annotations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Global Scope

A

declared in the main body
available from any scope
global variable
global keyword
namespace of __main__ module
module scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Local Scope

A

declare inside function
available within the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Nonlocal Variables

A

nonlocal keyword
used in nested functions
variable belongs to the outer function
variable refers to the previously bound variable in the closest enclosing scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Enclosing Scope

A

scope of the outer function
local scope of any enclosing function’s local scopes
nonlocal scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Built-in Scope

A

special reserved keywords

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

LEGB

A

Local
Enclosing
Global
Built-in

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

First-Class Function

A
  • assign to variable
  • pass as an argument
  • return from function

first-class citizen

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Higher-Order Function

A

a function that can take other functions as arguments or return functions as results

map, filter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Free Variable

A

variable that is used in a code block but not defined there

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Closure

A

function that retains access to variables from the enclosing scope even after the outer function has finished executing

closure factory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Decorator

A
def decorator(func):
    def wrapper(*args, **kwargs):
        ...
        result = func(*args, **kwargs)
        ...
        return result
    return wrapper

@functools.wraps(func)

17
Q

Pie-Decorator

A
@decorator
def func:
    ...

func = decorator(func)

18
Q

Decorator with optional arguments

A
def name(_func=None, *, key1=value1, key2=value2, ...):
    def decorator_name(func):
        ...
    if _func is None:
        return decorator_name
    else:
        return decorator_name(_func)
19
Q

Type Hints

A
def func(arg1: type1, arg2: type2 = default, ...) -> return_type:
    ...

mypy, pyright, typeguard