SlideShare a Scribd company logo
1 of 41
More Effective Python 2st
( -p
D0 4 c h
Decorator & Generator
7 1 GOKVSYRH
77 3IGSV SV
777 5IRIV SV
7C R0
목차
Background
• 코드 블럭 내에서 사용되었지만, 해당 블럭 내에 정의되지 않은 변수
• Global variable 과는 다른 개념임
Free variable
x is free variable
x is global variable x is local variable
x is free variable
y is local variable
• First Class (Citizen, Type, Object, Entity, Value)
• 다른 개체들이 이용 가능한 일반적인 기능들을 모두 지원하는 개체
First Class Citizen
• 일반적인 기능이란?
• | t r
• | r
• u r
First Class Citizen
,
• 대표적인 예로 scalar 데이터 타입은 거의 다 First Class Citizen
• 7R IKIV JPS RK TS R I G
• 일부 언어들에서, array와 string 은 First Class Citizen 이 아님
• | d | uw en | tw
First Class Citizen
-
• 함수를 First Class Citizen 처럼 다룰수 있다는 것을 의미
• s | t r
• s | r
• u q g r
• k q g r
First Class Function
.
First Class Function
• 다른 함수 안에 정의된 함수를 말함
• 내부 함수는 scope chain 에 의해 다음과 같은 특징을 가짐
• o y l r
• y l r
• y l r
• l r
Nested Function
• Nested Function 과 유사한 개념
• 내부 함수가 외부 함수가 종료된 후에도 외부 함수의 값을
참조하거나 동작 하는 행위를 말함
Closures
• Nested Function 가 Closures 를 포괄하는 큰 개념임
Nested Function 과 Closures
(
Nested Function : O
Closures : O
Nested Function : O
Closures : X
• Python 에서 Free variable 값이 어디 저장되는 지 알려줌
Closure in python
)
• 아래 조건 중 하나 이상을 만족하는 경우를 말함
• | n i q h
• | q h
• 비슷한 예시로 map 이나 callback 함수등이 있음
Higher Order Function
• 함수 인자의 일부를 미리 전달해 둔 함수를 생성하여 사용하는 것
• 함수의 여러개의 인자중 일부를 미리 입력 받아두어 사용하는 것
Partial Application
언어별 지원 현황
,
Language
Higher-order Functions Nested Functions Non-local variables
Arguments Results Named Anonymous Clousers Partial application
C Yes Yes No No No No
C++ Yes Yes C++11 C++11 C++11 C++11
Python Yes Yes Yes
Expressions
Only
Yes 2.5
PHP Yes Yes Yes 5.3 5.3 No
Decorator
• 기존의 클래스나 함수등을 수정하지 않고, 기능을 추가할 수 있음
• Decorator Pattern 과 Decorator 는 다른 개념
Decorator 란?
.
• “@” 키워드를 사용함
• RRS SRW j
• Python 2.6 에서 부터 Decorator 의 모든 기능을 지원하기 시작
• 2P WW q w
Decorator in Python
Decorator
(
Decorator
(
Single Decorator
((
Single Decorator
()
Multi Decorator
(
Multi Decorator
(
Multi Decorator
(,
Multi Decorator
(-
“@wrap” Annotation
(.
Generator
• Loop 의 Iteration 을 제어하는 특수한 로직
• 모든 Generator 는 Iterator 임
Generator 란?
)
• Generator 는 array 를 리턴하는 함수와 매우 유사
• 0VV q q x …
• Generator 는 한번에 하나의 값만 반환
• Generator 는 함수처럼 보이나, 실제로는 Iterator 의 역할을 수행
Generator 란?
)
• Generator 는 일반적으로 루프 안에서 호출 됨
• Generator 는 양보해뒀던(미뤄두었던) 값을 요구할 때만 처리함
• VI Q
• … m fen r WIUYIRGIW y
• v WIUYIRGIW n a H W VI Q
Generator 의 사용
)(
Generator (Iterator)
))
Generator
)
Generator with list comprehension (Iterator)
)
Generator with list comprehension
),
Generator with list comprehension
)-
Generator Performance
).
Generator Performance
)
QnA
L R O y o u
2ST V KL ( - IR IGYV W IQW 7RG 0PP V KL W VIWIV IH
9 40
B 0
80 0
EISY HS ISYP TIR WIGYV GS OV 6
6SYW SR AI W TIR WIGYV GSQ
L RNYOY 9Y ASO S TIR WIGYV GS NT

More Related Content

Similar to Effective Python 2st (Decorator & Generator)

C언어 세미나 - 함수
C언어 세미나 - 함수C언어 세미나 - 함수
C언어 세미나 - 함수SeungHyun Lee
 
[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기Sang Heon Lee
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features SummaryChris Ohk
 
Java 8 & Beyond
Java 8 & BeyondJava 8 & Beyond
Java 8 & BeyondJay Lee
 
Effective C++ Chaper 1
Effective C++ Chaper 1Effective C++ Chaper 1
Effective C++ Chaper 1연우 김
 
이클립스 플랫폼
이클립스 플랫폼이클립스 플랫폼
이클립스 플랫폼Kenu, GwangNam Heo
 
GopherCon Korea 2015 - Python 개발자를 위한 Go (이경찬)
GopherCon Korea 2015 - Python 개발자를 위한 Go (이경찬)GopherCon Korea 2015 - Python 개발자를 위한 Go (이경찬)
GopherCon Korea 2015 - Python 개발자를 위한 Go (이경찬)Kyoungchan Lee
 
2013 C++ Study For Students #1
2013 C++ Study For Students #12013 C++ Study For Students #1
2013 C++ Study For Students #1Chris Ohk
 
Scala, Scalability
Scala, ScalabilityScala, Scalability
Scala, ScalabilityDongwook Lee
 
Python 생태계의 이해
Python 생태계의 이해Python 생태계의 이해
Python 생태계의 이해용 최
 
파이썬 데이터과학 레벨1 - 초보자를 위한 데이터분석, 데이터시각화 (2020년 이태영)
파이썬 데이터과학 레벨1 - 초보자를 위한 데이터분석, 데이터시각화 (2020년 이태영) 파이썬 데이터과학 레벨1 - 초보자를 위한 데이터분석, 데이터시각화 (2020년 이태영)
파이썬 데이터과학 레벨1 - 초보자를 위한 데이터분석, 데이터시각화 (2020년 이태영) Tae Young Lee
 
JavaScript closure & scope
JavaScript closure & scopeJavaScript closure & scope
JavaScript closure & scopedaejoon
 
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기Chris Ohk
 
Swift 3 Programming for iOS : class and structure
Swift 3 Programming for iOS : class and structureSwift 3 Programming for iOS : class and structure
Swift 3 Programming for iOS : class and structureKwang Woo NAM
 
Start IoT with JavaScript - 6.함수
Start IoT with JavaScript - 6.함수Start IoT with JavaScript - 6.함수
Start IoT with JavaScript - 6.함수Park Jonggun
 
C Language For Arduino
C Language For ArduinoC Language For Arduino
C Language For Arduino영욱 김
 

Similar to Effective Python 2st (Decorator & Generator) (20)

C언어 세미나 - 함수
C언어 세미나 - 함수C언어 세미나 - 함수
C언어 세미나 - 함수
 
[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기
 
Linq
LinqLinq
Linq
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary
 
Java 8 & Beyond
Java 8 & BeyondJava 8 & Beyond
Java 8 & Beyond
 
Effective C++ Chaper 1
Effective C++ Chaper 1Effective C++ Chaper 1
Effective C++ Chaper 1
 
이클립스 플랫폼
이클립스 플랫폼이클립스 플랫폼
이클립스 플랫폼
 
GopherCon Korea 2015 - Python 개발자를 위한 Go (이경찬)
GopherCon Korea 2015 - Python 개발자를 위한 Go (이경찬)GopherCon Korea 2015 - Python 개발자를 위한 Go (이경찬)
GopherCon Korea 2015 - Python 개발자를 위한 Go (이경찬)
 
2013 C++ Study For Students #1
2013 C++ Study For Students #12013 C++ Study For Students #1
2013 C++ Study For Students #1
 
Scalability
ScalabilityScalability
Scalability
 
Scala, Scalability
Scala, ScalabilityScala, Scalability
Scala, Scalability
 
Python 생태계의 이해
Python 생태계의 이해Python 생태계의 이해
Python 생태계의 이해
 
파이썬 데이터과학 레벨1 - 초보자를 위한 데이터분석, 데이터시각화 (2020년 이태영)
파이썬 데이터과학 레벨1 - 초보자를 위한 데이터분석, 데이터시각화 (2020년 이태영) 파이썬 데이터과학 레벨1 - 초보자를 위한 데이터분석, 데이터시각화 (2020년 이태영)
파이썬 데이터과학 레벨1 - 초보자를 위한 데이터분석, 데이터시각화 (2020년 이태영)
 
JavaScript closure & scope
JavaScript closure & scopeJavaScript closure & scope
JavaScript closure & scope
 
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
 
Swift 3 Programming for iOS : class and structure
Swift 3 Programming for iOS : class and structureSwift 3 Programming for iOS : class and structure
Swift 3 Programming for iOS : class and structure
 
Start IoT with JavaScript - 6.함수
Start IoT with JavaScript - 6.함수Start IoT with JavaScript - 6.함수
Start IoT with JavaScript - 6.함수
 
Whats new rails 7
Whats new   rails 7Whats new   rails 7
Whats new rails 7
 
C Language For Arduino
C Language For ArduinoC Language For Arduino
C Language For Arduino
 
Anatomy of Realm
Anatomy of RealmAnatomy of Realm
Anatomy of Realm
 

More from 경섭 심

Python Packaging & Debian Packaging
Python Packaging & Debian PackagingPython Packaging & Debian Packaging
Python Packaging & Debian Packaging경섭 심
 
Debian packaging - Advanced
Debian packaging - AdvancedDebian packaging - Advanced
Debian packaging - Advanced경섭 심
 
Debian packaging - basic process
Debian packaging - basic processDebian packaging - basic process
Debian packaging - basic process경섭 심
 
Ubuntu packaging - deploy package
Ubuntu packaging - deploy packageUbuntu packaging - deploy package
Ubuntu packaging - deploy package경섭 심
 
Ubuntu packaging - make package
Ubuntu packaging - make packageUbuntu packaging - make package
Ubuntu packaging - make package경섭 심
 
More Effective Python 3st (Multitask)
More Effective Python 3st (Multitask)More Effective Python 3st (Multitask)
More Effective Python 3st (Multitask)경섭 심
 
Main Variable Program
Main Variable ProgramMain Variable Program
Main Variable Program경섭 심
 
Python comparing
Python comparingPython comparing
Python comparing경섭 심
 
Python coroutine
Python coroutinePython coroutine
Python coroutine경섭 심
 
Debian 패키지 관리 프로세스
Debian 패키지 관리 프로세스Debian 패키지 관리 프로세스
Debian 패키지 관리 프로세스경섭 심
 
Effective Python 1st (Test & Style)
Effective Python 1st (Test & Style)Effective Python 1st (Test & Style)
Effective Python 1st (Test & Style)경섭 심
 

More from 경섭 심 (11)

Python Packaging & Debian Packaging
Python Packaging & Debian PackagingPython Packaging & Debian Packaging
Python Packaging & Debian Packaging
 
Debian packaging - Advanced
Debian packaging - AdvancedDebian packaging - Advanced
Debian packaging - Advanced
 
Debian packaging - basic process
Debian packaging - basic processDebian packaging - basic process
Debian packaging - basic process
 
Ubuntu packaging - deploy package
Ubuntu packaging - deploy packageUbuntu packaging - deploy package
Ubuntu packaging - deploy package
 
Ubuntu packaging - make package
Ubuntu packaging - make packageUbuntu packaging - make package
Ubuntu packaging - make package
 
More Effective Python 3st (Multitask)
More Effective Python 3st (Multitask)More Effective Python 3st (Multitask)
More Effective Python 3st (Multitask)
 
Main Variable Program
Main Variable ProgramMain Variable Program
Main Variable Program
 
Python comparing
Python comparingPython comparing
Python comparing
 
Python coroutine
Python coroutinePython coroutine
Python coroutine
 
Debian 패키지 관리 프로세스
Debian 패키지 관리 프로세스Debian 패키지 관리 프로세스
Debian 패키지 관리 프로세스
 
Effective Python 1st (Test & Style)
Effective Python 1st (Test & Style)Effective Python 1st (Test & Style)
Effective Python 1st (Test & Style)
 

Recently uploaded

Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)Wonjun Hwang
 
캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차캐드앤그래픽스
 
MOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution DetectionMOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution DetectionKim Daeun
 
Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)Wonjun Hwang
 
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...Kim Daeun
 
A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)Tae Young Lee
 

Recently uploaded (6)

Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)
 
캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차
 
MOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution DetectionMOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution Detection
 
Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)
 
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
 
A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)
 

Effective Python 2st (Decorator & Generator)

  • 1. More Effective Python 2st ( -p D0 4 c h Decorator & Generator
  • 2. 7 1 GOKVSYRH 77 3IGSV SV 777 5IRIV SV 7C R0 목차
  • 4. • 코드 블럭 내에서 사용되었지만, 해당 블럭 내에 정의되지 않은 변수 • Global variable 과는 다른 개념임 Free variable x is free variable x is global variable x is local variable x is free variable y is local variable
  • 5. • First Class (Citizen, Type, Object, Entity, Value) • 다른 개체들이 이용 가능한 일반적인 기능들을 모두 지원하는 개체 First Class Citizen
  • 6. • 일반적인 기능이란? • | t r • | r • u r First Class Citizen ,
  • 7. • 대표적인 예로 scalar 데이터 타입은 거의 다 First Class Citizen • 7R IKIV JPS RK TS R I G • 일부 언어들에서, array와 string 은 First Class Citizen 이 아님 • | d | uw en | tw First Class Citizen -
  • 8. • 함수를 First Class Citizen 처럼 다룰수 있다는 것을 의미 • s | t r • s | r • u q g r • k q g r First Class Function .
  • 10. • 다른 함수 안에 정의된 함수를 말함 • 내부 함수는 scope chain 에 의해 다음과 같은 특징을 가짐 • o y l r • y l r • y l r • l r Nested Function
  • 11. • Nested Function 과 유사한 개념 • 내부 함수가 외부 함수가 종료된 후에도 외부 함수의 값을 참조하거나 동작 하는 행위를 말함 Closures
  • 12. • Nested Function 가 Closures 를 포괄하는 큰 개념임 Nested Function 과 Closures ( Nested Function : O Closures : O Nested Function : O Closures : X
  • 13. • Python 에서 Free variable 값이 어디 저장되는 지 알려줌 Closure in python )
  • 14. • 아래 조건 중 하나 이상을 만족하는 경우를 말함 • | n i q h • | q h • 비슷한 예시로 map 이나 callback 함수등이 있음 Higher Order Function
  • 15. • 함수 인자의 일부를 미리 전달해 둔 함수를 생성하여 사용하는 것 • 함수의 여러개의 인자중 일부를 미리 입력 받아두어 사용하는 것 Partial Application
  • 16. 언어별 지원 현황 , Language Higher-order Functions Nested Functions Non-local variables Arguments Results Named Anonymous Clousers Partial application C Yes Yes No No No No C++ Yes Yes C++11 C++11 C++11 C++11 Python Yes Yes Yes Expressions Only Yes 2.5 PHP Yes Yes Yes 5.3 5.3 No
  • 18. • 기존의 클래스나 함수등을 수정하지 않고, 기능을 추가할 수 있음 • Decorator Pattern 과 Decorator 는 다른 개념 Decorator 란? .
  • 19. • “@” 키워드를 사용함 • RRS SRW j • Python 2.6 에서 부터 Decorator 의 모든 기능을 지원하기 시작 • 2P WW q w Decorator in Python
  • 30. • Loop 의 Iteration 을 제어하는 특수한 로직 • 모든 Generator 는 Iterator 임 Generator 란? )
  • 31. • Generator 는 array 를 리턴하는 함수와 매우 유사 • 0VV q q x … • Generator 는 한번에 하나의 값만 반환 • Generator 는 함수처럼 보이나, 실제로는 Iterator 의 역할을 수행 Generator 란? )
  • 32. • Generator 는 일반적으로 루프 안에서 호출 됨 • Generator 는 양보해뒀던(미뤄두었던) 값을 요구할 때만 처리함 • VI Q • … m fen r WIUYIRGIW y • v WIUYIRGIW n a H W VI Q Generator 의 사용 )(
  • 35. Generator with list comprehension (Iterator) )
  • 36. Generator with list comprehension ),
  • 37. Generator with list comprehension )-
  • 40. QnA
  • 41. L R O y o u 2ST V KL ( - IR IGYV W IQW 7RG 0PP V KL W VIWIV IH 9 40 B 0 80 0 EISY HS ISYP TIR WIGYV GS OV 6 6SYW SR AI W TIR WIGYV GSQ L RNYOY 9Y ASO S TIR WIGYV GS NT