Functions Flashcards

1
Q

Arguments

A

positional *args
keyword **kwargs

variable-length (varargs):
*args, **kwargs

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)

required
optional

formal parameters

PEP 570

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
single expression
no type annotations

argument to higher-order function

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

Global Scope

A

global variable
global keyword
module scope

global variables are available from any scope except built-in

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

Local Scope

A

declared inside function
available within the function

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

Nonlocal Variable

A

nonlocal keyword
used in nested functions

defined in an outer (enclosing) scope but not in the global scope

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

Enclosing Scope

A

scope of outer function

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

__builtins__ module

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

in closure defined in enclosing scope

can be global if the nearest enclosing scope contains a global statement

can be nonlocal

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

function.__closure__

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

Top-level code

A

first user-specified module that starts running

__name__ == ‘__main__’

entry point

20
Q

Decorator with arguments

A
def name(args):
    def decorator_name(func):
        ...
    return decorator_name