4.7.3.임의 인수 목록
Arbitary Argument Lists
Finally, the least frequently used option is to specify that a function can be called with an arbitrary number of arguments. These arguments will be wrapped up in a tuple (see Tuples and Sequences). Before the variable number of arguments, zero or more normal arguments may occur.
마지막으로, 가장 드물게 사용되는 방식은 어떤 함수가 임의의 개수를 가진 인수를 가지도록 지정하는 것입니다. 이 인수들은 튜플 안에 들어있게 됩니다. (튜플과 시퀀스 참조. 아직 연결되지 않음) 개수가 정해지지 않은 이 인수 앞에는 일반적인 인수가 배치될 수도 있습니다.
def write_multiple_items(file, separator, *args):
file.write(separator.join(args))
Normally, these variadic arguments will be last in the list of formal parameters, because they scoop up all remaining input arguments that are passed to the function. Any formal parameters which occur after the *args
parameter are ‘keyword-only’ arguments, meaning that they can only be used as keywords rather than positional arguments.
일반적으로, 이러한 가변인수가 형식매개변수의 마지막 종류입니다. 왜냐하면 이 가변인수들이 함수에 전달된 나머지 모든 입력인수들을 가져가기 때문입니다. *args
매개변수 뒤에 오는 다른 모든 형식매개변수들은 '키워드 전용' 인수입니다. 키워드 전용 인수란 위치인수가 아닌 키워드만 사용할 수 있는 인수입니다.
>>> def concat(*args, sep="/"):
... return sep.join(args)
...
>>> concat("earth", "mars", "venus")
'earth/mars/venus'
>>> concat("earth", "mars", "venus", sep=".")
'earth.mars.venus'