๐Ÿงญ SyntaxError: positioanl argument follows keyword argument

Min-Ho Leeยท2020๋…„ 6์›” 25์ผ
1

Learn Python

๋ชฉ๋ก ๋ณด๊ธฐ
11/22
post-thumbnail

Thursday_June_25
.
.
.
์•ž์„œ ์ œ๋ชฉ์˜ ๋ฌธ๋ฒ• ์—๋Ÿฌ๋ฅผ ์ดํ•ดํ•˜๋ ค๋ฉด ๋จผ์ € ๊ฐœ๋…์„ ์žก์•„์•ผ ํ•œ๋‹ค.

1๏ธโƒฃ Positional Arguments

๊ธฐ๋ณธ์ ์ธ ํ•จ์ˆ˜์˜ Parameter์—๊ฒŒ ๊ฐ’์„ ์ „๋‹ฌํ•ด ์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค.

def number(first, second, third):
  print("First = ", first)
  print("Second = ", second)
  print("Third = ", third)

์ด๋Ÿฐ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ ํ›„, ํ˜ธ์ถœ์„ ํ•  ๋•Œ
Argument์—์„œ ์ฃผ๋Š” ์ˆœ์„œ๋Œ€๋กœ ๊ฐ’์ด Parameter์—๊ฒŒ ์ „๋‹ฌ์ด ๋œ๋‹ค.

number("st", "nd", "rd")

์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

First =  st
Second =  nd
Third =  rd

๋‹ค์Œ๊ณผ ๊ฐ™์ด Argument๋ฅผ ์ฃผ๋ฉด

number(789, 456, 123)

์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

First =  789
Second =  456
Third =  123

2๏ธโƒฃ Keyword Arguments

Parameter์—๊ฒŒ ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์ง€ ์•Š๋Š” ๋Œ€์‹ ์—
ํ•จ์ˆ˜์—์„œ ์ •์˜๋œ Parameter์˜ ์ด๋ฆ„์œผ๋กœ ๋งž์ถ”์–ด์„œ ๊ฐ’์„ ์ „๋‹ฌํ•ด ์ฃผ๋Š” ๋ฐฉ์‹์„ ๋งํ•œ๋‹ค.

์ด ๋ฐฉ์‹์˜ ์žฅ์ ์€ Parameter์˜ ์ด๋ฆ„์„ ๋งž์ถ”์–ด ์ฃผ๊ธฐ๋•Œ๋ฌธ์—,
ํ˜ธ์ถœ์„ ํ•˜๋Š” Argument๋“ค์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ ์ƒ๊ด€์ด ์—†๋‹ค๋Š” ์ ์ด๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด๋ฆ„์„ ๋งž์ถ”์–ด ๊ฐ’์„ ๋„˜๊ธฐ๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค Parameter์— ์–ด๋–ค ๊ฐ’์„ ์ฃผ๋Š”์ง€ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

def number(first, second, third):
  print("First = ", first)
  print("Second = ", second)
  print("Third = ", third)

๋˜‘๊ฐ™์€ ์˜ˆ์ œ์—์„œ Keyword Argument ๋ฐฉ์‹์„ ์จ๋ณด๊ฒ ๋‹ค.

number(third = "rd", first = "st", second = "nd")

์ด๋ ‡๊ฒŒ ์ˆœ์„œ๋ฅผ ๋’ค์ฃฝ๋ฐ•์ฃฝ ์„ž์—ˆ์–ด๋„ Parameter์˜ ์ด๋ฆ„์— ๋งž์ถ”์–ด ๊ฐ’์„ ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์—

First =  st
Second =  nd
Third =  rd

์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๊ตฟ์ด๋‹ค ๐Ÿ‘

3๏ธโƒฃ Mixing 1๏ธโƒฃ + 2๏ธโƒฃ

Positional Argument ๋ฐฉ์‹๊ณผ Keyword Argument ๋ฐฉ์‹์„ ์„ž์–ด ์“ฐ๋Š” ๋ฐฉ์‹์ด๋‹ค.

์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•ด๋ณด์ž.

def real_madrid(ramos, kroos, benzema, bale):
  print("Ramos's Back Number = ", ramos)
  print("Kroos's Back Number = ", kroos)
  print("Benzema's Back Number = ", benzema)
  print("Bale's Back Number = ", bale)

โฐ TMI Time

๋ผ๋ชจ์Šค์˜ ๋“ฑ๋ฒˆํ˜ธ๋Š” 4๋ฒˆ
ํฌ๋กœ์Šค์˜ ๋“ฑ๋ฒˆํ˜ธ๋Š” 8๋ฒˆ
๋ฒค์ œ๋งˆ์˜ ๋“ฑ๋ฒˆํ˜ธ๋Š” 9๋ฒˆ
๋ฒ ์ผ์˜ ๋“ฑ๋ฒˆํ˜ธ๋Š” 11๋ฒˆ์ด๋‹ค.

์•„๋ฌดํŠผ ๋‘ ๋ฐฉ์‹์„ ์„ž์–ด์„œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์ž.

real_madrid(4, 8, benzema = 9, bale = 11)
real_madrid(4, 8, bale = 11, benzema = 9)

์ผ๋‹จ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’์€ ๋‘˜ ๋‹ค ๋ชจ๋‘ ๊ฐ™๋‹ค.

Ramos's Back Number =  4
Kroos's Back Number =  8
Benzema's Back Number =  9
Bale's Back Number =  11

๊ทธ๋ฆฌ๊ณ  ์œ„์—์„œ 4์™€ 8์€ Positional ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด๊ณ ,
benzema์™€ bale์€ Keyword ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด๋‹ค.
๊ทธ๋ž˜์„œ ์•ž์„œ ์„ค๋ช…ํ–ˆ๋“ฏ, Keyword Argument ๋ฐฉ์‹์€ ์ˆœ์„œ๊ฐ€ ์ƒ๊ด€์ด ์—†๊ธฐ ๋•Œ๋ฌธ์—, ์ฒซ ๋ฒˆ์งธ ์ค„๊ณผ ๋‘ ๋ฒˆ์งธ ์ค„์—์„œ ๋ณด์ด๋Š”๊ฒƒ ์ฒ˜๋Ÿผ benzema์™€ bale์˜ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟ”๋„ ์ƒ๊ด€์ด ์—†๋‹ค.

โ—๏ธ SyntaxError: positioanl argument follows keyword argument

3๋ฒˆ์˜ Mixing ๋ฐฉ๋ฒ•์˜ ์ฃผ์˜์‚ฌํ•ญ์ด ์žˆ๋‹ค.

Keyword ๋ผ๋ฆฌ์˜ ์ˆœ์„œ๋Š” ์ƒ๊ด€์ด ์—†์ง€๋งŒ,
Positional๊ณผ Keyword๋ฅผ ์„ž์–ด ์“ธ ๋•Œ์—๋Š” ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค.

EX)

real_madrid(4, kroos = 8, 9, bale = 11)
real_madrid(ramos = 4, kroos = 8, 9, 11)

๋‘ ๋ฐฉ์‹ ๋ชจ๋‘ ๋‹ค ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

SyntaxError: positioanl argument follows keyword argument

Keyword ๋ฐฉ์‹์ด Positional ๋ฐฉ์‹๋ณด๋‹ค ์•ž์—์„œ ์‚ฌ์šฉ๋๋‹ค๊ณ  ๋œฌ ์—๋Ÿฌ์ด๋‹ค.

๊ทธ๋ƒฅ ์„ž์–ด์„œ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ๋ฌด์กฐ๊ฑด ์•ž์ชฝ์— Positional ๋ฐฉ์‹์„ ์จ์ค€ ํ›„, Keyword ๋ฐฉ์‹์„ ์จ์ฃผ๋ฉด ๋œ๋‹ค.

profile
๐Ÿ‡ Rabbit can take a rest, but ๐Ÿข turtle can't

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