๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

#IT #Python9

[Python] ๋ฌธ์ž์—ด์„ ์ „๋‹ฌ ๋ฐ›์•„์„œ ๋ฌธ์ž ํ•˜๋‚˜์”ฉ ์ถ”์ถœํ•˜์—ฌ ๋ณ€ํ™˜ํ•˜๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ(Iterator) ์ƒ์„ฑํ•˜๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„๋ฅผ ํƒ๊ตฌํ•ฉ์‹œ๋‹ค. ๋ฌธ์ž์—ด์„ ์ „๋‹ฌ ๋ฐ›์•„์„œ ๋ฌธ์ž ํ•˜๋‚˜์”ฉ ์ถ”์ถœํ•˜์—ฌ ๋ณ€ํ™˜ํ•˜๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ(Iterator) ์ƒ์„ฑํ•˜๊ธฐ ### ์ดํ„ฐ๋ ˆ์ดํ„ฐ ํด๋ž˜์Šค ์ƒ์„ฑํ•ด์„œ Hello ๊ฐ ๋‹จ์–ด ์ถœ๋ ฅํ•˜๊ธฐ # - ํด๋ž˜์Šค ์ด๋ฆ„ : StringIterator # - ์ž„์˜ ๋ฌธ์ž์—ด์„ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. # - ์ž„์˜ ๋ฌธ์ž์—ด์€ ์™ธ๋ถ€์—์„œ ํด๋ž˜์Šค ์ƒ์„ฑ์‹œ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. class StringIterator: ### ํด๋ž˜์Šค ์ƒ์„ฑ์ž def __init__(self, p_text) : # - nextํ•จ์ˆ˜์—์„œ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ ๋ฐ˜๋ณต ์กฐ๊ฑด์— ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜ self.index = 0 # - ๋ฐ›์•„์˜จ ๋ฌธ์ž์—ด self.text = p_text ### ๋ฐ˜๋ณต์ˆ˜ํ–‰์„ ์œ„ํ•œ iter ํ•จ์ˆ˜ ์ •์˜ def __iter__(self) : return self ### ํ•œ๊ฑด ํ•œ๊ฑด .. 2023. 11. 18.
[Python] ์ดํ„ฐ๋ ˆ์ดํ„ฐ(Iterator)_ํ”„๋กœ๊ทธ๋žจ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„๋ฅผ ํƒ๊ตฌํ•ฉ์‹œ๋‹ค. ํŒŒ์ด์ฌ์—์„œ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด(ํด๋ž˜์Šค)๋ฅผ ํ‘œํ˜„ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋Š” iter()ํ•จ์ˆ˜์™€ next()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ˜๋ณต(Inerator)์„ ์ˆ˜ํ–‰ ### ํด๋ž˜์Šค ์ •์˜ํ•˜๊ธฐ class MyIterator : ### ํด๋ž˜์Šค ์ƒ์„ฑ์ž ์ •์˜ํ•˜๊ธฐ def __init__(self) : self.current_value = 0 print(f"#1(__init__) : self = {self} / self.current_value={self.current_value}") ### ์ž์‹ ์˜ ํด๋ž˜์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” iter ํ•จ์ˆ˜ ์ •์˜ def __iter__(self) : print(f"#2(__iter__) : self = {self}") return self ### ๋ฐ˜๋ณต์„ ์ˆ˜ํ–‰ํ•˜๋Š” next .. 2023. 11. 17.
[Python] ํด๋กœ์ €(Closure) ํ”„๋กœ๊ทธ๋žจ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„๋ฅผ ํƒ๊ตฌํ•ฉ์‹œ๋‹ค. - ํ•จ์ˆ˜ ์•ˆ์— ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹ - ํ•จ์ˆ˜ ์•ˆ์— ์žˆ๋Š” ํ•จ์ˆ˜๋Š” ๋ฐ”๊นฅ์ชฝ ํ•จ์ˆ˜์—์„œ ์ฐธ์กฐํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. - ํ•จ์ˆ˜ ์•ˆ์— ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉ์ด ๋๋‚˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ•ด์ œ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค. ํด๋กœ์ € ํ•จ์ˆ˜ ์ •์˜ํ•˜๊ธฐ ### ํด๋กœ์ € ํ•จ์ˆ˜ ์ •์˜ํ•˜๊ธฐ def outer_function(x) : print(f"#1 : x = {x}") ### ๋‚ด๋ถ€ ํ•จ์ˆ˜ ์ •์˜ : ์‹ค์ œ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜ def inner_function(y) : print(f"#2 : y = {y}") s = x + y print(f"#3 : s = {s}") return s print("#4 -------") return inner_function ํด๋กœ์ € ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๊ธฐ ### ํด๋กœ์ € ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๊ธฐ.. 2023. 11. 14.
[Python] ์œ ๋‹ˆ์ฝ”๋“œ ํ”„๋กœ๊ทธ๋žจ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„๋ฅผ ํƒ๊ตฌํ•ฉ์‹œ๋‹ค. - ์œ ๋‹ˆ์ฝ”๋“œ(Unicode)๋Š” ์ „ ์„ธ๊ณ„์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ํ‘œ์ค€ ์ธ์ฝ”๋”ฉ ์ฒด๊ณ„ - ์œ ๋‹ˆ์ฝ”๋“œ ํ”„๋กœ๊ทธ๋žจ : ์–ธ์–ด, ๋ฌธ์ž, ํŠน์ˆ˜๊ธฐํ˜ธ ๋“ฑ์˜ ํ‘œ์ค€ ๋ฌธ์ž์—ด ๊ฒฐํ•ฉํ•˜๊ธฐ ### ๋ฌธ์ž์—ด ๊ฒฐํ•ฉํ•˜๊ธฐ string1 = "ํŒŒ์ด์ฌ(python)" string2 = "์žฌ๋ฏธ์žˆ์–ด์š”" string3 = "!!!" print(string1 + string2 + string3) ๋ฌธ์ž์—ด์„ UTF-8๋กœ ์ธ์ฝ”๋”ฉํ•˜๊ธฐ ### ๋ฌธ์ž์—ด์„ UTF-8๋กœ ์ธ์ฝ”๋”ฉํ•˜๊ธฐ text = "์•ˆ๋…•ํ•˜์„ธ์š”" encode_text = text.encode("utf-8") encode_text ๋ฌธ์ž์—ด์„ UTF-8๋กœ ๋””์ฝ”๋”ฉํ•˜๊ธฐ ### ๋ฌธ์ž์—ด์„ UTF-8๋กœ ๋””์ฝ”๋”ฉํ•˜๊ธฐ decode_text = encode_text.decode("utf-8") d.. 2023. 11. 14.

loading