Python에 별표(* 또는 **)가 있는 함수 매개변수 상세 정보

1. 기본값이 있는 매개변수


별표(*)가 있는 매개변수를 이해하기 전에 기본값이 있는 매개변수를 살펴보십시오. 함수는 다음과 같이 정의됩니다.

>> def defaultValueArgs(common, defaultStr = "default", defaultNum = 0):
    print("Common args", common)
    print("Default String", defaultStr)
    print("Default Number", defaultNum) 
 
(1) 기본값이 있는 매개변수(defaultStr, defaultNum)가 참조되지 않을 때 호출:

>> defaultValueArgs("Test")
 
Common args Test
Default String default
Default Number 0
 
(2) 기본값이 있는 매개 변수 (defaultStr, defaultNum) 는 호출할 때 직접 전달할 수 있습니다 (아래의 defaultStr). "argsName = value"형식 (아래의 defaultNum).

>> defaultValueArgs("Test", "Str", defaultNum = 1)
 
Common args Test
Default String Str
Default Number 1
 
>> defaultValueArgs("Test", defaultNum = 1)
 
Common args Test
Default String default
Default Number 1
주의: 함수를 정의할 때, 첫 번째 기본값이 있는 매개 변수 이후의 모든 매개 변수는 기본값이 있어야 합니다. 그렇지 않으면 타임즈 오류가 실행됩니다.

>> def defaultValueArgs(common, defaultStr = "default", defaultNum):
    print("Common args", common)
    print("Default String", defaultStr)
    print("Default Number", defaultNum)
    
SyntaxError: non-default argument follows default argument
 


2. 별표(*)가 있는 함수 매개변수


매개변수가 있는 함수는 다음과 같이 정의됩니다.

>> def singalStar(common, *rest):
  print("Common args: ", common)
    print("Rest args: ", rest)
(1) 별표(*)가 있는 매개변수는 참조되지 않습니다.

>> singalStar("hello")
 
Common args: hello
Rest args: ()
별표 (*) 가 있는 매개변수는 참조되지 않을 때 기본적으로 비어 있는 메타그룹입니다.
(2) 별표(*)가 있는 매개변수가 여러 값으로 전달될 때(함수 정의보다 크거나 같은 매개변수 개):

>> singalStar("hello", "world", 000)
 
Common args: hello
Rest args: ('world', 0)
두 번째 방식에서 별표 파라미터는 수신된 여러 파라미터를 하나의 원조로 통합하는 것을 알 수 있다.
(3) 메타그룹 유형의 값을 별표 매개변수에 직접 전달할 때:

>> singalStar("hello", ("world", 000))
 
Common args: hello
Rest args: (('world', 0),)
이때 전달된 메타그룹 값은 별표 매개 변수의 메타그룹 중의 한 요소이다.
(4) 만약 우리가 원조를 별표 매개 변수의 매개 변수 값으로 삼고 싶다면, 원조 값 앞에 "*"를 붙이면 된다.

>> singalStar("hello", *("world", 000))
Common args: hello
Rest args: ('world', 0)

>> singalStar("hello", *("world", 000), "123")
Common args: hello
Rest args: ('world', 0, '123')

3. 별표 두 개 (**) 가 있는 함수 매개변수


두 개의 별표(**)가 있는 함수는 다음과 같이 정의됩니다.

>> def doubleStar(common, **double):
    print("Common args: ", common)
    print("Double args: ", double)
(1) 2성 번호 (**) 매개변수는 값을 전달하지 않습니다.

>> doubleStar("hello")
 
Common args: hello
Double args: {}
별표 (**) 가 있는 매개변수는 값을 전달하지 않을 때 기본적으로 빈 사전입니다.
(2) 쌍성호(**) 매개변수가 여러 매개변수에 전달될 때(함수 정의보다 크거나 같은 매개변수 개수):

>> doubleStar("hello", "Test", 24)
TypeError: doubleStar() takes 1 positional argument but 3 were given

>> doubleStar("hello", x = "Test", y = 24)
Common args: hello
Double args: {'x': 'Test', 'y': 24}

쌍성호 매개 변수는 수신된 여러 매개 변수를 하나의 사전으로 통합하는 것을 볼 수 있지만, 단성호와 달리 기본값인 "args=value"방식을 사용해야 합니다. "="앞의 필드는 사전의 키가 되고, "="뒤의 필드는 사전의 값이 됩니다.
(3) 사전을 별표 매개 변수의 매개 변수 값으로 하려면 어떻게 해야 합니까?단성호 매개변수와 유사하게 사전 값 앞에 "**"를 붙이고 그 이후에는 값을 추가할 수 없습니다.

>> doubleStar("hello", {"name": "Test", "age": 24})
TypeError: doubleStar() takes 1 positional argument but 2 were given

>> doubleStar("hello", **{"name": "Test", "age": 24}, {"name": "Test2", "age": 24})
SyntaxError: positional argument follows keyword argument unpacking

>> doubleStar("hello", **{"name": "Test", "age": 24}, **{"name": "Test2", "age": 24})
TypeError: doubleStar() got multiple values for keyword argument 'name'

>> doubleStar("hello", **{"name": "Test", "age": 24})
Common args: hello
Double args: {'name': 'Test', 'age': 24}

4. 어떤 경우 단성호 함수 파라미터와 쌍성호 함수 파라미터는 함께 사용된다.


def singalAndDoubleStar(common, *single, **double):
  print("Common args: ", common)
  print("Single args: ", single)
  print("Double args: ", double)

singalAndDoubleStar("hello")
# Common args: hello
# Single args: ()
# Double args: {}
singalAndDoubleStar("hello", "world", 000)
# Common args: hello
# Single args: ('world', 0)
# Double args: {}
singalAndDoubleStar("hello", "world", 000, {"name": "Test", "age": 24})
# Common args: hello
# Single args: ('world', 0, {'name': 'Test', 'age': 24})
# Double args: {}
singalAndDoubleStar("hello", "world", 000, **{"name": "Test", "age": 24})
# Common args: hello
# Single args: ('world', 0)
# Double args: {'name': 'Test', 'age': 24}
singalAndDoubleStar("hello", ("world", 000), {"name": "Test", "age": 24})
# Common args: hello
# Single args: (('world', 0), {'name': 'Test', 'age': 24})
# Double args: {}
singalAndDoubleStar("hello", *("world", 000), {"name": "Test", "age": 24}) 
# Common args: hello
# Single args: ('world', 0, {'name': 'Test', 'age': 24})
# Double args: {}
singalAndDoubleStar("hello", *("world", 000), **{"name": "Test", "age": 24})
# Common args: hello
# Single args: ('world', 0)
# Double args: {'name': 'Test', 'age': 24}
Python 별표 (* 또는 **) 의 함수 매개 변수에 대한 자세한 설명은 여기 있습니다. 더 많은 Python 별표 매개 변수에 대한 내용은 저희 이전의 글을 검색하거나 아래의 관련 글을 계속 훑어보십시오. 앞으로 많은 응원 부탁드립니다!

좋은 웹페이지 즐겨찾기