4.7.4. Unpacking Argument Lists

The reverse situation occurs when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments. For instance, the built-in range() function expects separate start and stop arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:
인수가 이미 리스트나 튜플 안에 포함되어 있지만 분리된 위치 인수를 요구하는 함수를 위해 풀려나와야 할 때는 반대의 상황이 발생합니다. 예를 들어, 내장함수인 range()에 시작 인수와 끝 인수가 필요하다고 칩시다. 만약 시작 인수와 끝 인수가 분리될 수 없다면, 함수를 *표시와 함께 사용해 리스트나 튜플 안에서 끄집어 내야 합니다.

>>> list(range(3, 6))            # 일반적인 호출 & 분리된 각각의 인수들
[3, 4, 5]
>>> args = [3, 6]
>>> list(range(*args))            # 리스트에서 분리되지 않은 인수들을 불러내는 호출
[3, 4, 5]

In the same fashion, dictionaries can deliver keyword arguments with the **-operator:
같은 방식으로, 딕셔너리는 ** 표시와 함께 키워드 인수를 제공합니다.

>>> def parrot(voltage, state='a stiff', action='voom'):
...     print("-- This parrot wouldn't", action, end=' ')
...     print("if you put", voltage, "volts through it.", end=' ')
...     print("E's", state, "!")
...
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !

results matching ""

    No results matching ""