ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/Python

[Python] ๋ฐ”๋‹ค์ฝ”๋ผ๋ฆฌ ์—ฐ์‚ฐ์ž walrus operator

NaNaRin๐Ÿ™ƒ 2021. 4. 28. 19:28

ํŒŒ์ด์ฌ 3.8์— ๋„์ž…๋œ ๋ฐ”๋‹ค์ฝ”๋ผ๋ฆฌ ์—ฐ์‚ฐ์ž := := :=

 

1. ํ˜•์‹

์ด๋ฆ„ := ํ‘œํ˜„์‹

 

2. ๋ชฉ์ 

ํ‘œํ˜„์‹์— ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๊ณ  ์žฌ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ

๋ฐ”๋‹ค์ฝ”๋ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ• ๋‹น๊ณผ ํ…Œ์ŠคํŠธ๋ฅผ ํ•œ ๋‹จ๊ณ„๋กœ ์ค„์ผ ์ˆ˜ ์žˆ์Œ

 

3. ์‚ฌ์šฉ ์˜ˆ

walrus = "walrus operator"
n1 = len(walrus)
if n1 > 5:
	print("kkk")
    
if n2 := len(walrus) > 5:
	print("kkk")

=> 2,3 ๋ฒˆ ๋ผ์ธ์˜ ๋‘ ๋‹จ๊ณ„๋ฅผ 6๋ฒˆ ๋ผ์ธ ํ•œ ๋‹จ๊ณ„๋กœ ์ค„์ž„

+ for๋ฌธ, while๋ฌธ ๋“ฑ์—๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅ