ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 위치인자(Positional Argument) & 키워드인자(Keyword Argument) & 기본인자(Default Argument)
    Python/Basic 2022. 9. 27. 09:01

    1. 위치 인자(Positional Argument)

    def print_numbers(a,b,c):
        print(b,a,c)
        
    print_numbers(10,20,30)   # 20 10 30
    
    
    # 인수를 순서대로 넣을 때는 리스트나 튜플을 사용 할 수 있다 
    함수(*리스트)
    함수(*튜플)    
    
    print_numbers(*[10, 20, 30]) # 20 10 30

    # 위치 인수와 리스트 언패킹은 어디에 사용하나?!

    - 인수의 갯수가 정해지지 않은 가변 인수(variable arguement)에 사용한다 

    def 함수이름(*매개변수):
        코드
        
        
    def print_numbers(*args):
        for arg in args:
            print(arg)
            
            
    # 함수에 직접 값들을 넣어주거나?!        
    print_numbers(10)
    print_numbers(10,20,30,40)
    
    
    # 리스트 or 튜플에 언팩킹을 사용해도 됨 
    x = [10]
    print_numbers(*x)
    
    y = [10,20,30,40]
    print_numbers(*y)

    # 고정인수(fixed args) 랑 가변인수(flexible args) 함께 줄 때  

    def print_numbers(a, *args):
        print("고정인수입니당!",a)
        print("가변인수입니당",args)
        
    print_numbers(1)
    print_numbers(1,10,20)
    print_numbers(*[10, 20, 30])
    
    고정인수입니당! 1
    가변인수입니당 ()
    ================================
    고정인수입니당! 1
    가변인수입니당 (10, 20)
    ================================
    고정인수입니당! 10
    가변인수입니당 (20, 30)
    
    # print_numbers(*args, a):처럼 *args가 고정 매개변수보다 앞쪽에 오면 안 된다

     

    2. 키워드(keyword)인자

    def personal_info(name, age, address):
        print('이름: ', name)
        print('나이: ', age)
        print('주소: ', address)
        
        
    # 키워드 인수는 말 그대로 인수에 이름(키워드)을 붙이는 기능인데 키워드=값 형식으로 사용
    # 함수(키워드=값)
    
    
    #  personal_info 함수를 키워드 인수 방식으로 호출해보자 
    personal_info(name='조연아', age=30, address='경기도부천시')

     

     

    3. default 인자

    # default 인자 지정하기 
    
    '''
    def 함수이름(매개변수=값):
        코드
    '''
    
    def personal_info(name, age, address='비공개'):
        print('이름', name)
        print('나이', age)
        print('주소', address)
        
    personal_info('조연아',30)
    personal_info('조연아',30, '경기도 부천')
    
    # 초기값이 지정된 매개 변수 다음에는 초기값이 없는 매겨변수가 올 수 없다 
    >>> def personal_info(name, address='비공개', age):
        ...
        
    # 따라서 default가 지정된 것은 뒤로 몰아주면 된다 
    def personal_info(name, age, address='비공개'):
    def personal_info(name, age=100, address='비공개'):
    def personal_info(name='비공개', age=100, address='비공개'):

     

    실습

     

    def get_max_score(*scores):
        subjects = list()
        subjects.append(scores)
        # subjects 리스트 [100,86,81,91]로 들어가는 것이 아니라 [(100,86,81,91)] 이렇게 들어간다. list[0]값에 4개의 인자가 다 들어감 
        max_score = max(subjects)
        return max_score 
        
        
    korean, english, mathematics, science = 100, 86, 81, 91
    
    max_score = get_max_score(korean, english, mathematics, science)
    print('높은 점수:', max_score) # 높은 점수: (100, 86, 81, 91)
    
    
    
    solution 
    
    def get_max_score(*scores):
        return max(scores)       
        
    korean, english, mathematics, science = 100, 86, 81, 91
    
    max_score = get_max_score(korean, english, mathematics, science)
    print('높은 점수:', max_score) # 높은 점수: (100, 86, 81, 91)

    'Python > Basic' 카테고리의 다른 글

    __getitem__  (0) 2022.10.13
    데코레이터(Decorator)  (0) 2022.10.12
    람다(Lambda)와 클로저(Closure)  (1) 2022.09.29
    타입힌팅(Type Hinting)  (0) 2022.09.27
    s.split() vs s.split(" ")  (0) 2022.09.17

    댓글

Designed by Tistory.