Thursday_June_25
.
.
.

๐Ÿ“š Variable Length Arguments (๊ฐ€๋ณ€ ์ธ์ž)

Positional Arguments ํ˜น์€! Keyword Arguments๋ฐฉ์‹์œผ๋กœ
Parameter์—๊ฒŒ ๋„˜๊ฒจ์ค„ ๊ฐ’์ด ๋„ˆ๋ฌด ๋งŽ๊ฑฐ๋‚˜, ๊ฐœ์ˆ˜๊ฐ€ ์ •ํ•ด์ง€์ง€ ์•Š์•˜์„ ๋•Œ
์‚ฌ์šฉ์„ ํ•œ๋‹ค.

๊ทธ๋ž˜์„œ ๊ฐ€๋ณ€์ธ์ž์˜ ์ข…๋ฅ˜๋„ ์ •ํ™•ํžˆ 2๊ฐœ๋กœ ๋‚˜๋‰œ๋‹ค.

๋‹ค์‹œ ๋˜ ๊ฟ€๋ง›๊ฐ™์€ ์„ค๋ช…์„ ์œ„ํ•ด ์‚ฌ์ง„์„ ๋„์šฐ๊ฒ ๋‹ค.

1๏ธโƒฃ Variable Length Positional Arguments

์ž„์˜์˜ ๊ฐœ์ˆ˜์˜ Positional Arguments ๋ฐฉ์‹์œผ๋กœ, Tuple ํ˜•ํƒœ์˜ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•œ๋‹ค.

EX)

def test(a, b, c, *args):
    print(a)
    print(b)
    print(c)
    print(args)

test(1, 2, 3, 4, 5, 6)

ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ˜ธ์ถœ์„ ํ•˜๋ฉด
Positional Arguments ๋ฐฉ์‹์œผ๋กœ ๊ฐ’์ด ์ „๋‹ฌ์ด ๋˜๋ฉด์„œ
๋‹น์—ฐํžˆ a์™€ b์™€ c์—๋Š” ๊ฐ๊ฐ 1๊ณผ 2์™€ 3์ด ์ „๋‹ฌ์ด ๋  ๊ฒƒ์ด๊ณ ,
๊ฐ€๋ณ€์˜ ๊ธธ์ด์˜ ๊ฐ’์„ ๋ฐ›๋Š” args๋ผ๋Š” Parameter๋Š” 4์™€ 5์™€ 6์„
Tuple์˜ ํ˜•ํƒœ๋กœ ๋ฐ›๊ฒŒ ๋œ๋‹ค.
(ํ•จ์ˆ˜๋ฅผ ์ •์˜ ํ•  ๋•Œ๋งŒ *๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค.)

์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1
2
3
(4, 5, 6)

2๏ธโƒฃ Variable Length Keyword Arguments

์ž„์˜์˜ ๊ฐœ์ˆ˜์˜ Keyword Arguments ๋ฐฉ์‹์œผ๋กœ, Dictionary ํ˜•ํƒœ์˜ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•œ๋‹ค.

EX)

def test(a, b, **kwargs):
  print(a)
  print(b)
  print(kwargs)
test(1, 2, name = "Lee", age = 23, company = "Wecode")

๋จผ์ € ํ˜ธ์ถœ ๋ถ€๋ถ„์˜ 1๊ณผ 2๋Š” ๋‹น์—ฐํžˆ Positional Argument ๋ฐฉ์‹์œผ๋กœ a์™€ b์—๊ฒŒ ๊ฐ’์„ ๋„˜๊ฒจ์คฌ๋‹ค.

์—ฌ๊ธฐ์„œ๋Š” 1๊ณผ 2๊ฐ€ a์™€ b์—๊ฒŒ Positional Argument ๋ฐฉ์‹์œผ๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•ด ์ฃผ์—ˆ๊ณ , ket="value" ์˜ ํ˜•ํƒœ๋กœ kwargs๋ผ๋Š” Parameter์—๋Š”
name = "Lee", age = 23, company = "Wecode" ๋ผ๋Š” ๊ฐ’์ด Dictionary ํ˜•ํƒœ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

(์•„๊นŒ์™€ ๊ฐ™์ด ์ด ๋ฐฉ์‹๋„ ์ •์˜ ํ•  ๋•Œ๋งŒ ๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค.)**

์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1
2
{'name': 'Lee', 'age': 23, 'company': 'Wecode'}

๐Ÿ“ ์ดํ•ดํ•˜๊ธฐ

๋ฌธ์ œ๋“ค์„ ํ’€๋ฉด์„œ ๐Ÿ‘‰ Positional Argument, Keyword Argument, Variable Length Positional Argument, Variable Length Keyword Argument์— ๋Œ€ํ•ด ์ข€ ๋” ์ตํ˜€๋ณด๊ฒ ๋‹ค.

1๏ธโƒฃ ๋ฌธ์ œ

def exam1(name, *args, age):
    print("name=",end=""), print(name)
    print("args=",end=""), print(args)
    print("age=",end=""), print(age)

exam1("์ •์šฐ์„ฑ", "01012341234", "seoul", 20)

โ“ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ์ด์œ 

์ด ๋ฌธ์ œ์—์„œ๋Š” Positional Argument ๋ฐฉ์‹์œผ๋กœ
name์—๋Š” ์ •์šฐ์„ฑ, args์—๋Š” ๋ฒˆํ˜ธ์™€ ์ฃผ์†Œ๋ฅผ, age์—๋Š” 20์„ ์ฃผ๋ ค๊ณ  ํ•œ ๊ฒƒ ๊ฐ™์€๋ฐ, ์ผ๋‹จ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ๋ถ€๋ถ„์—์„œ๋Š” ์—๋Ÿฌ๊ฐ€ ์—†์„ ๊ฒƒ์ด๋‹ค.

์ € ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด
name์€ non-default positional parameter์ด๊ณ ,
*args๋Š” Variable Length Positional Parameter์ด๊ณ ,
age๋Š” non-default keyword parameter ์—ฌ์„œ
์ •์˜๋ฅผ ํ•˜๋Š” ๋ฐ์—๋Š” ์—๋Ÿฌ๊ฐ€ ์—†๋‹ค.

ํ•˜์ง€๋งŒ ํ˜ธ์ถœ ๋ถ€๋ถ„์—์„œ ์ž˜๋ชป ๋˜์—ˆ๋‹ค.

args๋Š” ๊ฐ€๋ณ€์˜ ๊ธธ์ด๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ๋†ˆ์ธ๋ฐ,
๋ฒˆํ˜ธ์™€ ์„œ์šธ, ๊ทธ๋ฆฌ๊ณ  20๊นŒ์ง€๋ฅผ ๊ฐ€๋ณ€์˜ ๊ฐ’์œผ๋กœ ์ธ์‹์„ ํ•ด์„œ 3๊ฐœ์˜ ๊ฐ’ ๋ชจ๋‘ ๋“ค์–ด๊ฒŒ๊ฐ€ ๋  ๊ฒƒ์ธ๋ฐ, ๊ทธ๋Ÿฌ๋ฉด age๋ผ๋Š” parameter์—๊ฒŒ๋Š” ๊ฐ’์ด ์—†๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งํ–ˆ๋“ฏ์ด age๋Š” Keyword๋กœ ๊ฐ’์„ ์ค˜์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.

๐Ÿคฉ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

1. ํ˜ธ์ถœ ๋ถ€๋ถ„์—์„œ 20์„ age=20 ์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

def exam1(name, *args, age):
    print("name=",end=""), print(name)
    print("args=",end=""), print(args)
    print("age=",end=""), print(age)

exam1("์ •์šฐ์„ฑ", "01012341234", "seoul", age = 20)

์ถœ๋ ฅ ๊ฒฐ๊ณผ

name=์ •์šฐ์„ฑ
args=('01012341234', 'seoul')
age=20

2. age์™€ 20์˜ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟ”์ค€๋‹ค.

def exam1(name, age, *args):
    print("name=",end=""), print(name)
    print("args=",end=""), print(args)
    print("age=",end=""), print(age)

exam1("์ •์šฐ์„ฑ", 20, "01012341234", "seoul")

์ถœ๋ ฅ ๊ฒฐ๊ณผ

name=์ •์šฐ์„ฑ
args=('01012341234', 'seoul')
age=20

Argument์—์„œ ์ˆœ์„œ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ๋„˜๊ฒจ์ฃผ์–ด๋„ ํ•จ์ˆ˜์•ˆ์—์„œ print๋ฌธ ๋กœ์ง์€ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ๊ฐ™๋‹ค.

2๏ธโƒฃ ๋ฌธ์ œ

def exam2(name, age, **kwargs, address=0):
    print("name=",end=""), print(name)
    print("age=",end=""), print(age)
    print("kwargs=",end=""), print(kwargs)
    print("address=",end=""), print(address)

exam2("์ •์šฐ์„ฑ", "20", mobile="01012341234", address="seoul")

โ“ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ์ด์œ 

์ผ๋‹จ ์ด ๊ตฌ์กฐ๋Š” parameter ์ˆœ์„œ๋Œ€๋กœ

non-dafault positional
non-dafault positional
variable Length Keyword
default keyword
์ธ๋ฐ,

์ด๊ฑด ํ•จ์ˆ˜ ์ •์˜ ์กฐ์ฐจ ์•ˆ ๋  ๊ฒƒ์ด๋‹ค.

parameter ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด ์•ˆ๋˜๋Š” ์ด์œ ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

๐Ÿคฉ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

1 - 1. address parameter๋ฅผ kwargs parameter ์•ž์œผ๋กœ ์˜ฎ๊ฒจ์ค€๋‹ค.

def exam2(name, age, address=0, **kwargs):
    print("name=",end=""), print(name)
    print("age=",end=""), print(age)
    print("kwargs=",end=""), print(kwargs)
    print("address=",end=""), print(address)

exam2("์ •์šฐ์„ฑ", "20", mobile="01012341234", address="seoul")

ํ•จ์ˆ˜ ์ •์˜ ๋ถ€๋ถ„์—์„œ๋Š” ๋ฌธ๋ฒ•์ ์œผ๋กœ ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด ์—†์œผ๋‹ˆ ํŒจ์Šค.

ํ˜ธ์ถœ ๋ถ€๋ถ„์—์„œ argument ๋ถ€๋ถ„์„ ๋ณด๋ฉด,
์ •์šฐ์„ฑ๊ณผ 20์€ positional ๋ฐฉ๋ฒ•์œผ๋กœ, mobile๊ณผ address๋Š” keyword๋กœ ์ฃผ์—ˆ๋‹ค.
keyword ๋ฐฉ์‹์€ ์ˆœ์„œ๊ฐ€ ์ƒ๊ด€ ์—†๊ธฐ๋•Œ๋ฌธ์—, ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™์ด ๋œ๋‹ค.

์ถœ๋ ฅ ๊ฒฐ๊ณผ

name=์ •์šฐ์„ฑ
age=20
kwargs={'mobile': '01012341234'}
address=seoul

1 - 2.
1๋ฒˆ๊ณผ ๊ฐ™์€ ์ •์˜๋œ ํ•จ์ˆ˜์—์„œ, ์ถœ๋ ฅ ๋ถ€๋ถ„๋งŒ ๋ฐ”๊ฟ”์„œ ๋‹ค๋ฅด๊ฒŒ ์ž‘๋™ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

parameter๋Š” address ๊นŒ์ง€ positional ๋ถ€๋ถ„ ์ด์–ด์„œ,
๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ˜ธ์ถœํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

def exam2(name, age, address=0, **kwargs):
    print("name=",end=""), print(name)
    print("age=",end=""), print(age)
    print("kwargs=",end=""), print(kwargs)
    print("address=",end=""), print(address)

exam2("์ •์šฐ์„ฑ", "20", "seoul", mobile="01012341234")

์ด๋ ‡๊ฒŒ "seoul" argument๋ฅผ positional argument ๋ฐฉ์‹์œผ๋กœ ์ค˜๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

๋ฌผ๋ก  ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋„ ๊ฐ™๋‹ค.

์ถœ๋ ฅ ๊ฒฐ๊ณผ

name=์ •์šฐ์„ฑ
age=20
kwargs={'mobile': '01012341234'}
address=seoul

3๏ธโƒฃ ๋ฌธ์ œ

def exam3(name="์•„์ด์œ ", *args, age, **kwargs, address):
    print("name=",end=""), print(name)
    print("args=",end=""), print(args)
    print("age=",end=""), print(age)
    print("kwargs=",end=""), print(kwargs)
    print("address=",end=""), print(address)

exam3(20, "์ •์šฐ์„ฑ", "01012341234", "male" ,mobile="01012341234", address="seoul")

โ“ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ์ด์œ 

ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ๋ถ€ํ„ฐ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.
parameter ์ˆœ์„œ๋Œ€๋กœ ๋ณด๋ฉด

default positional
variable length positional
non-dafault keyword
variable length keyword
non-dafault keyword
์ธ๋ฐ,

๋งˆ์ง€๋ง‰ parameter์ธ address๋งŒ age์™€ kwargs ์‚ฌ์ด์— ๋„ฃ์–ด์ฃผ๋ฉด
์ •์˜ ๋ถ€๋ถ„์—์„œ๋Š” ์˜ค๋ฅ˜๊ฐ€ ์•ˆ๋‚œ๋‹ค.

์ถœ๋ ฅ ๋ถ€๋ถ„๋งŒ positional์ธ์ง€ keyword ์ธ์ง€ ์ž˜ ์ƒ๊ฐํ•ด์„œ ์ „๋‹ฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๐Ÿคฉ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

def exam3(name="์•„์ด์œ ", *args, age, address, **kwargs):
    print("name=",end=""), print(name)
    print("args=",end=""), print(args)
    print("age=",end=""), print(age)
    print("kwargs=",end=""), print(kwargs)
    print("address=",end=""), print(address)

exam3(20, "์ •์šฐ์„ฑ", "01012341234", "male" ,mobile="01012341234", address="seoul")

์ผ๋‹จ ์ด๋ ‡๊ฒŒ ์ •์˜ ๋ถ€๋ถ„์—์„œ์˜ ์—๋Ÿฌ๋งŒ ์ˆ˜์ •ํ•˜๊ณ , ์ถœ๋ ฅ ๋ถ€๋ถ„์œผ๋กœ ํ•ด๊ฒฐ์„ ๋ณด๊ฒ ๋‹ค.

1. ๐Ÿ˜ฉ
๊ณ ์น ๊ฒŒ ๋„ˆ๋ฌด ๋งŽ๋‹ค.
์ผ๋‹จ ์œ„์— ์ฒ˜๋Ÿผ ์ž˜๋ชป๋œ ์ถœ๋ ฅ์„ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •์„ ํ•˜๋ฉด,
20์€ name์œผ๋กœ, ์ •์šฐ์„ฑ์€ ๊ฐ€๋ณ€ parameter์ธ args์— ์—ฌ๊ธฐ์„œ ์ปดํ“จํ„ฐ๋Š”
๋‡Œ์ •์ง€๊ฐ€ ์˜จ๋‹ค. ๋ฒˆํ˜ธ๋„ args์ธ์ง€ ์–ด๋–ค argument ๊นŒ์ง€ args์— ๋„ฃ์„์ง€ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

exam3("์ •์šฐ์„ฑ", "male", "01012341234", age=20, address="seoul", mobile="01012341234")

๋‹ต์ด ์ด๊ฑฐ ๋ง๊ณ ๋Š” ์—†๋‹ค. ์œ ์ผํ•œ ๋‹ต์ด๋‹ค. (๋ฌผ๋ก  ๋‚ด ๋จธ๋ฆฌ์˜ ๊ธฐ์ค€์ด๋‹ค)

๋ฌผ๋ก  ์—ฌ๊ธฐ์„œ age, address, kwargs ์ด 3๊ฐœ์˜ parameter ๋ผ๋ฆฌ๋Š”
keyword argument ๋ฐฉ์‹์œผ๋กœ ๋„˜๊ฒจ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์„ž์–ด๋„ ์ƒ๊ด€์—†๋‹ค.

์ถœ๋ ฅ ๊ฒฐ๊ณผ

name=์ •์šฐ์„ฑ
args=('male', '01012341234')
age=20
kwargs={'mobile': '01012341234'}
address=seoul
profile
๐Ÿ‡ Rabbit can take a rest, but ๐Ÿข turtle can't

0๊ฐœ์˜ ๋Œ“๊ธ€