Better Way 3. bytes์ str ์ฐจ์ด๋ฅผ ์์๋๋ผ
# ์๋ชป๋ ์
print(b"hello" + "world") # TypeError ๋ฐ์
# ์ฌ๋ฐ๋ฅธ ์
print(b"hello" + b"world") # bytes๋ผ๋ฆฌ ๊ฒฐํฉ ๊ฐ๋ฅ
print("hello" + "world") # str๋ผ๋ฆฌ ๊ฒฐํฉ ๊ฐ๋ฅ
bytes์ str์ ๊ฒฐํฉ์ด ๋ถ๊ฐ๋ฅ
# ํ
์คํธ ๋ชจ๋๋ก ํ์ผ ์ฝ๊ธฐ (str ๋ฐํ)
with open("example.txt", "r", encoding="utf-8") as f:
text = f.read()
print(text)
# ๋ฐ์ด๋๋ฆฌ ๋ชจ๋๋ก ํ์ผ ์ฝ๊ธฐ (bytes ๋ฐํ)
with open("example.txt", "rb") as f:
binary_data = f.read()
print(binary_data)
encode(), decode() ๊ธฐ๋ณธ๊ฐ์ utf-8 ์ด๋ฉฐ, ์ด๋ฅผ ์ฌ์ฉํ์.
Better Way 5. ๋ณต์กํ ์ ๋์ ๋์ฐ๋ฏธ ํจ์๋ฅผ ์ฌ์ฉํ๋ผ
# ๋ณต์กํ ์กฐ๊ฑด๋ฌธ (๋์ ์)
if user.is_active and user.role == "admin" and user.has_permission("edit"):
do_something()
# ๋์ฐ๋ฏธ ํจ์ ํ์ฉ (์ข์ ์)
def is_admin_user(user):
return user.is_active and user.role == "admin" and user.has_permission("edit")
if is_admin_user(user):
do_something()
Better Way 6. ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ๋ ๋์ ๋์ ์ ์ฌ์ฉํด ๋ฐ์ดํฐ๋ฅผ ์ธํจํนํ๋ผ
# ๋ฆฌ์คํธ์์ ์ธ๋ฑ์ค๋ก ๊ฐ ํ ๋น (๋์ ์)
data_list = [["Alice", 25, "Developer"], ["Bob", 30, "Designer"], ["Charlie", 35, "Manager"]]
for data in data_list:
name = data[0]
age = data[1]
job = data[2]
print(f"{name} is {age} years old and works as a {job}.")
# ์ธํจํน ์ฌ์ฉ (์ข์ ์)
for name, age, job in data_list:
print(f"{name} is {age} years old and works as a {job}.")
Better Way 7. range ๋ณด๋ค๋ enumerate๋ฅผ ์ฌ์ฉํ๋ผ
# range ์ฌ์ฉ (๋์ ์)
items = ["apple", "banana", "cherry"]
for i in range(len(items)):
print(f"{i+1}: {items[i]}")
# enumerate ์ฌ์ฉ (์ข์ ์)
for i, item in enumerate(items, 1): # ๋๋ฒ์งธ ์ธ์๋ก ์์ ์ธ๋ฑ์ค ์ง์ ๊ฐ๋ฅ - default๋ 0
print(f"{i}: {item}")
>> 1: apple
>> 2: banana
>> 3: cherry
Better Way 8. ์ฌ๋ฌ ์ดํฐ๋ ์ดํฐ์ ๋ํด ๋๋ํ ๋ฃจํ๋ฅผ ์ํํ๋ ค๋ฉด zip์ ์ฌ์ฉํ๋ผ
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
# ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ ์
for i in range(len(names):
print(f"{names[i]} is {ages[i]} years old.")
# zip์ ์ฌ์ฉํ ์
for name, age in zip(names, ages):
print(f"{name} is {age} years old.")
๋ง์ฝ์ ๊ธธ์ด๊ฐ ์๋ก ๋ค๋ฅธ ์ํ์ค ๊ฐ์ฒด๋ผ๋ฉด ์ด๋ป๊ฒ ๋ ๊น?
names = ["Alice", "Bob", "Charlie", "Delta"] # ๊ธธ์ด๊ฐ 4
ages = [25, 30, 35] # ๊ธธ์ด๊ฐ 3
# zip์ ์ฌ์ฉํ ์
for name, age in zip(names, ages):
print(f"{name} is {age} years old.")
>> Alice is 25 years old.
Bob is 30 years old.
Charlie is 35 years old.
์งง์ ์ดํฐ๋ ์ดํฐ ๊ธฐ์ค์ผ๋ก ๋ฐ๋ณต๋ฌธ์ ์ํํ ํ, ์ข ๋ฃ๋๋ค.
๋ด๋ถ ๊ตฌ์กฐ๋ฅผ ์๊ฐํ๋ฉด for ๋ฌธ์ด ์ดํฐ๋ ์ดํฐ์ next() ํธ์ถํ๋ค๊ฐ StopIteration์ ๋ง๋๋ฉด ์ข ๋ฃํ๊ฒ ๋๋, ์งง์์ชฝ์์ ๋จผ์ StopIteration์ ๋ฐ์ํ ํ, ์ข ๋ฃํ๊ฒ ๋๋ค.
๋ง์ฝ ๊ธด ์ดํฐ๋ ์ดํฐ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฐ๋ณต๋ฌธ์ ์ํํ๊ณ ์ถ๋ค๋ฉด
from itertools import zip_longest
names = ["Alice", "Bob", "Charlie", "Delta"] # ๊ธธ์ด๊ฐ 4
ages = [25, 30, 35] # ๊ธธ์ด๊ฐ 3
# zip์ ์ฌ์ฉํ ์
for name, age in zip_longest(names, ages):
print(f"{name} is {age} years old.")
>> Alice is 25 years old.
Bob is 30 years old.
Charlie is 35 years old.
Delta is None years old.
๋ด์ฅ ํจ์์ธ zip_longest๋ฅผ ์ฌ์ฉํ๋ฉด ๊ธด ์ดํฐ๋ ์ดํฐ ๊ธฐ์ค์ผ๋ก ๋ฐ๋ณต๋ฌธ์ ์ํ๋๊ณ , ๊ธธ์ด๊ฐ ๋ชจ์๋ ๊ฒฝ์ฐ None ์ ๋ฐํํ๋ค.
Better Way 10. ๋์ ์์ ์ฌ์ฉํด ๋ฐ๋ณต์ ํผํ๋ผ
# ๋์
์์ ์ฌ์ฉํ์ง ์์ ์ฝ๋ (๋์ ์)
value = input("Enter something: ")
while value:
print(f"You entered: {value}")
value = input("Enter something: ")
# ๋์
์ ์ฌ์ฉ (์ข์ ์)
while (value := input("Enter something: ")):
print(f"You entered: {value}")
:= (์๋ฌ์ค) ์ฐ์ฐ์๋ก ๊ฐ์ ๋ฃ์ด์ฃผ๋ฉฐ, ๋น๊ต ์ฐ์ฐ์ด ๊ฐ๋ฅํ๋ค.
ํด๋น ๊ฐ์ ํด๋น ์ค์ฝํ์์ ์ฌ์ฉ๊ฐ๋ฅ
Effective Python 2nd ์ดํํฐ๋ธ ํ์ด์ฌ : ํ์ด์ฌ ์ฝ๋ฉ์ ๊ธฐ์ : ๋ค์ด๋ฒ ๋์
๋ค์ด๋ฒ ๋์ ์์ธ์ ๋ณด๋ฅผ ์ ๊ณตํฉ๋๋ค.
search.shopping.naver.com
'๐ Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ดํํฐ๋ธ ํ์ด์ฌ 3์ฅ - ํจ์ (0) | 2025.03.17 |
---|---|
์ดํํฐ๋ธ ํ์ด์ฌ 2์ฅ - ๋ฆฌ์คํธ์ ๋์ ๋๋ฆฌ (0) | 2025.03.12 |
[Python] *, ** ์ฌ์ฉ๋ฒ (0) | 2025.02.18 |
[Python] GIL์ ํด์ ํด๋ณด์ (0) | 2024.10.11 |
[Python] ํ์ด์ฌ ๊ฐ๋ฐ์ ๋ฉด์ ์ง๋ฌธ (0) | 2024.05.26 |