Open
Conversation
rover0811
reviewed
Oct 17, 2025
| #기존 반복문을 사용한 리스트 작성 | ||
| square = [] | ||
| for x in range(10): | ||
| squares.append(x**2) |
|
|
||
| #리스트 컴프리헨션을 사용한 리스트 생성 | ||
| squares_comp = [x**2 for x in range(10)] | ||
| print(squares_comp) |
Member
There was a problem hiding this comment.
중첩 컨프리헨션 예시도 한번 생각해보세요
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): |
Member
There was a problem hiding this comment.
두 가지는 완전히 다른 개념이에요
# 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()를 호출한 쪽으로 전달하게 됨. 종료되는 것이 아니라 그 상태로 유지 | ||
|
|
||
| → 함수 내부에서 사용된 데이터들이 메모리에 유지 |
Member
There was a problem hiding this comment.
데이터가 메모리에 유지되면 기존 변수랑은 뭐가 다를까요? 설명을 덧붙여주세요
|
|
||
| : 객체를 문자열로 반환 | ||
|
|
||
| : interface로서의 역할을 수행하기 위해 존재 |
Member
There was a problem hiding this comment.
str: 최종 사용자를 위한 읽기 쉬운 출력
repr: 개발자를 위한 명확하고 재생성 가능한 표현
|
|
||
| → 개발자에 초점 | ||
|
|
||
| - `__eq__` |
Member
There was a problem hiding this comment.
__eq__만 구현하면 문제 발생하고 추가적으로 __hash__도 구현해야 합니다. 이유가 무엇일까요?
| func() | ||
| print("데코레이터가 추가한 내용") | ||
| return wrapper | ||
|
|
Member
There was a problem hiding this comment.
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 (원하는 건 "인사 함수입니다")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
학습 내용 요약
파이썬 문법인 컴프리헨션, 언패킹·제너레이터, 매직 메서드, 데코레이터는 코드의 간결성·효율성·재사용성을 높여준다.
핵심 개념
컴프리헨션은 반복문과 조건문을 한 줄로 표현한다.
제너레이터는 메모리를 절약하며 데이터를 지연 생성한다.
매직 메서드와 데코레이터는 객체의 동작과 함수의 기능을 확장해 가독성과 유지보수성을 향상시킨다.
실습 예제
참고 자료
[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)
다른 자료들은 각각에 명시함
체크리스트