Skip to content

Conversation

@SeoYeongBaek
Copy link
Collaborator

학습 내용 요약

파이썬 문법인 컴프리헨션, 언패킹·제너레이터, 매직 메서드, 데코레이터는 코드의 간결성·효율성·재사용성을 높여준다.

핵심 개념

  • 컴프리헨션은 반복문과 조건문을 한 줄로 표현한다.

  • 제너레이터는 메모리를 절약하며 데이터를 지연 생성한다.

  • 매직 메서드와 데코레이터는 객체의 동작과 함수의 기능을 확장해 가독성과 유지보수성을 향상시킨다.

실습 예제

참고 자료

[Python Tutorial - Unpacking](https://docs.python.org/ko/3.13/tutorial/controlflow.html#unpacking-argument-lists)

[Python Tutorial - Iterator, Generator](https://docs.python.org/ko/3.13/tutorial/classes.html#iterators)

[Python Reference -Magic method](https://docs.python.org/ko/3.13/reference/datamodel.html#special-method-names)

다른 자료들은 각각에 명시함

체크리스트

  • 주제에 대한 핵심 내용을 다루고 있다
  • 실습 가능한 코드 예제가 포함되어 있다
  • 마크다운 문법이 올바르게 적용되었다
  • 참고 자료 출처가 명시되어 있다

@SeoYeongBaek SeoYeongBaek changed the title Add Chapter1: pythonic code notes (Session1) Session1: pythonic code Oct 11, 2025
@SeoYeongBaek SeoYeongBaek changed the title Session1: pythonic code Session1: SeoYeongBaek Oct 11, 2025
#기존 반복문을 사용한 리스트 작성
square = []
for x in range(10):
squares.append(x**2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

작동하는 코드인가요?


#리스트 컴프리헨션을 사용한 리스트 생성
squares_comp = [x**2 for x in range(10)]
print(squares_comp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

중첩 컨프리헨션 예시도 한번 생각해보세요

result = [x*y for x in range(10) if x%2==0 for y in range(10) if y%3==0 if x+y<10]


```python
#가변인자 언패킹
def sum(a,b,c):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

두 가지는 완전히 다른 개념이에요

# 1. 함수 정의에서 *args (가변 매개변수)
def func(*args):  # 여러 개를 받아서 튜플로 묶음
    print(args)

func(1, 2, 3)  # (1, 2, 3)

# 2. 함수 호출에서 *list (언패킹)
def sum(a, b, c):  # 정확히 3개 필요
    return a + b + c

numbers = [1, 2, 3]
sum(*numbers)  # 리스트를 풀어서 전달 (1, 2, 3)


: iterator를 생성해 주는 함수, 일반함수와 차이는 generator함수가 실행 중 yield를 만날 경우, 해당 함수는 그 상태로 정지 되며 반환값을 next()를 호출한 쪽으로 전달하게 됨. 종료되는 것이 아니라 그 상태로 유지

→ 함수 내부에서 사용된 데이터들이 메모리에 유지
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

데이터가 메모리에 유지되면 기존 변수랑은 뭐가 다를까요? 설명을 덧붙여주세요


: 객체를 문자열로 반환

: interface로서의 역할을 수행하기 위해 존재
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str: 최종 사용자를 위한 읽기 쉬운 출력
repr: 개발자를 위한 명확하고 재생성 가능한 표현


→ 개발자에 초점

- `__eq__`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__eq__만 구현하면 문제 발생하고 추가적으로 __hash__도 구현해야 합니다. 이유가 무엇일까요?

func()
print("데코레이터가 추가한 내용")
return wrapper

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

functools.wraps에 대해 찾아보면 좋을 것 같아요.

def my_decorator(func):
	def wrapper():
		print("데코레이터가 추가한 내용")
		func()
	return wrapper

@my_decorator
def hello():
	"""인사 함수입니다"""
	print("안녕하세요")

# 문제 발생!
print(hello.__name__)  # 'wrapper' (원하는 건 'hello')
print(hello.__doc__)   # None (원하는 건 "인사 함수입니다")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants