ν•¨μˆ˜ / *args, **kwargs / generator, yield


ν•¨μˆ˜


  • ν•¨μˆ˜λž€? μž…λ ₯κ°’μ„λ°›μ•„μ„œμΆœλ ₯κ°’μ„λ‚΄μ–΄λ†“λŠ”κ²ƒ

    • μž…λ ₯ => [ν•¨μˆ˜] => 좜λ ₯
  • κΈ°λ³Έν˜•νƒœ

      def ν•¨μˆ˜λͺ…(λ§€κ°œλ³€μˆ˜):
          μˆ˜ν–‰ν•  λ¬Έμž₯1
          μˆ˜ν–‰ν•  λ¬Έμž₯2
          ...
    



*args / **kwargs


  • λ§€κ°œλ³€μˆ˜(parameter) / 인수(arguments)

    • λ§€κ°œλ³€μˆ˜ : ν•¨μˆ˜μ— μž…λ ₯으둜 μ „λ‹¬λœ 값을 λ°›λŠ” λ³€μˆ˜

    • 인수 : ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•Œ μ „λ‹¬ν•˜λŠ” μž…λ ₯κ°’

      def add(a, b)   # λ§€κ°œλ³€μˆ˜
          return a + b
    
      print(add(1, 2))    # 인수
    
  • *args : μž…λ ₯값이 λͺ‡ κ°œκ°€ 될 지 λͺ¨λ₯Ό λ•Œ

      def ν•¨μˆ˜λͺ…(*λ§€κ°œλ³€μˆ˜){
          μˆ˜ν–‰ν•  λ¬Έμž₯
          ...
      }
    
    • μΈμˆ˜λŠ” νŠœν”Œ ν˜•νƒœλ‘œ λ³€ν™˜, λ§€κ°œλ³€μˆ˜μ— μ €μž₯됨

        def test(*args):
            for i in args:
                print(i)
        test(1, 2, 3, 4)
      
        ### 좜λ ₯κ²°κ³Ό
        ### 1
        ### 2
        ### 3
        ### 4
      


        def add_sub(choice, *args):
            if choice == "add":
                res = 0
                for i in args:
                    res = res + i
            elif choice == "sub":
                res = 1
                for i in args:
                    res = res - i
            return res
      
        res = add_sub('add', 1, 2, 3, 4, 5)
        print(res)
        res = add_sub('sub', 10, 1)
        print(res)
      
        ### 좜λ ₯κ²°κ³Ό
        ### 15
        ### -10
      


        a, *b = [1, 2, 3, 4]
        print(a)
        print(b)
        ### 좜λ ₯κ²°κ³Ό
        ### 1
        ### [2, 3, 4]
      
      
      
        def test(a, *b):
            return a, b
      
        print(test(1, 2, 3, 4))
        ### 좜λ ₯κ²°κ³Ό
        ### (1, (2, 3, 4))
      
  • **kwargs

    • μΈμˆ˜λŠ” λ”•μ…”λ„ˆλ¦¬ ν˜•νƒœλ‘œ λ³€ν™˜, λ§€κ°œλ³€μˆ˜μ— μ €μž₯됨

    • (ν‚€μ›Œλ“œ = κ°’) ν˜•νƒœλ‘œλ§Œ ν•¨μˆ˜ 호좜 κ°€λŠ₯

        def info(**kwargs):
            print(kwargs)
      
        info(name="이감자", age="20")
      
        ### 좜λ ₯κ²°κ³Ό
        ### {'name': '이감자', 'age': '20'}
      



λ§€κ°œλ³€μˆ˜μ— μ΄ˆκΉƒκ°’ μ„€μ •ν•˜κΈ°


  • def ν•¨μˆ˜λͺ…(λ§€κ°œλ³€μˆ˜=κ°’):
        μˆ˜ν–‰ν•  λ¬Έμž₯
    
  • μ΄ˆκΈ°ν™”μ‹œν‚€κ³  싢은 λ§€κ°œλ³€μˆ˜λŠ” 항상 맨 뒀에 μœ„μΉ˜ν•΄μ•Ό ν•œλ‹€. 그렇지 μ•ŠμœΌλ©΄ μ—λŸ¬ λ°œμƒ

      def add(a, b=10):
          print(a+b)
    
      add(5)
      ### 좜λ ₯ > 15
      add(5, 5)
      ### 좜λ ₯ > 10
    
    
      ### def add(a=10, b):
      ###    print(a+b)
      ### > μ—λŸ¬!
    



generator / yield


  • generator : λ©”λͺ¨λ¦¬λ₯Ό 효율적으둜 μ‚¬μš©ν•˜λ©΄μ„œ λ°˜λ³΅μ„ μˆ˜ν–‰ν•˜λ„λ‘ λ•λŠ” 객체

  • yield : μ›ν•˜λŠ” κ°’ 리턴, μ‹€ν–‰ 흐름을 μΌμ‹œ μ •μ§€ν•˜μ—¬ ν•¨μˆ˜λ₯Ό μž¬ν™œμš©ν•  수 μžˆλŠ” μƒνƒœλ‘œ λ§Œλ“¦

      def gen(n):
          while n>0:
              yield n
              n=n-1
    
      print(gen(10))
    
      ### 좜λ ₯κ²°κ³Ό
      ### <generator object gen at 0x000001CED6C637B0>
      ### => generator 객체 λ°˜ν™˜
    
      print(list(gen(10)))
    
      ### 좜λ ₯κ²°κ³Ό
      ### [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
    


      def gen(nums):
          for num in nums:
              yield num * num
    
      nums = [i for i in range (6, 11)]
      res = gen(nums)
      for num in res:
          print(num)
    
      ### 좜λ ₯κ²°κ³Ό
      ### 36
      ### 49
      ### 64
      ### 81
      ### 100
    

Categories:

python