SlideShare uma empresa Scribd logo
1 de 34
PYTHON
SPECIAL
METHOD
이해하기
Moon Yong Joon
TYPE
Moon Yong Joon
Type special method3
메타클래스란
파이썬에서는 모든 클래스는 메타클래스에 의해 만
들어진다. 내장 메타클래스는 type이 제공되면 이
를 상속받아 새로운 메타클래스를 만들 수 있음
4
인스턴스 클래스 메타클래스
Instance of Instance of
type 메타클래스 정의
type 메타클래스로 클래스를 생성하려면 문자열로
클래스명을 정의하고, 상속관계를 tuple,
namespace에 dict 타입으로 선언
5
type(‘클래스명’,상속관계,namespace)
type __new__/__init__
type 메타클래스 생성은 __new__로 하고 __init__
으로 초기화 처리
6
type __new__/__init__
type 메타클래스로 클래스를 생성하려면 문자열로
클래스명을 정의하고, 상속관계를 tuple,
namespace에 dict 타입으로 선언 __init__ 첫번째
인자는 class가 와야 함
7
MyKlass = type.__new__(type, name, bases, dict)
type.__init__(MyKlass, name, bases, dict)
<class '__main__.MyKlass'> {'__module__': '__main__', '__dict__': <attribute
'__dict__' of 'MyKlass' objects>, '__doc__': None, '__weakref__': <attribute
'__weakref__' of 'MyKlass' objects>}
type __call__
type 메타클래스로 클래스를 생성하려면 문자열로
클래스명을 정의하고, 상속관계를 tuple,
namespace에 dict 타입으로 선언 __init__ 첫번째
인자는 class가 와야 함
8
MyKlass = type.__call__(type, name, bases, dict)
<class '__main__.MyKlass'>
{'__module__': '__main__', '__dict__': <attribute '__dict__' of 'MyKlass' objects>,
'__doc__': None, '__weakref__': <attribute '__weakref__' of 'MyKlass' objects>}
type.__prepare__
Once the appropriate metaclass has been
identified, then the class namespace is
prepared.
9
__prepare__(‘클래스명’,상속관계)
type.__prepare__ 처리예시
type 클래스 내의 __prepare__ 메소드를 이용해서
namespace를 결과로 받음
10
메타 클래스 정의 : 실행 결과
type을 상속받는 Meta라는 클래스를 생성
type.__new__ 메소드를 통해 새로운 클래스 생성
11
OBJECT
Moon Yong Joon
object special method
객체생성/초기화/소멸
Class의 인스턴스를 생성하는 메소드
object.__new__(cls[, ...])
object.__init__(self[, ...])
object.__del__(self)
생성
초기화
소멸
생성, 초기화,소멸 예시
Class 정의시 초기화만 정의해도 클래스의 인스턴
스를 생성할 수 있음
클래스.__new__/__init__
class A의 __new__, __init__ 메소드를 이용해서 인
스턴스를 생성하기
16
인스턴스 생성시 자동처리
class A를 호출하면 자동으로 __new__, __init__ 메
소드를 이용해서 인스턴스를 생성하기
17
__str__ & __repr__ 차이
출력
Str은 문자열 처리하지만 Repr은 문자열의 문자열
로 처리가 됨
Eval 함수 실행
두개의 차이는 별로 없지만 str 타입일 경우 repr로
처리시는 str 타입도 한번 더 str로 처리
객체 내부 속성 관리
속성 생성/변경/검색/소멸
Class의 속생을 생성, 변경, 검색, 소멸을 시키는 메
소드
object.__getattribute__(self, name)
object.__setattr__(self, name, value)
object.__delattr__(self, name)
검색
생성/변경
소멸
getattr(object, name[, default])
setattr(object, name, value)
delattr(object, name)
내장 함수
속성 생성/변경/검색/소멸 예시
Class로 인스턴스를 만들고 gettattribute로 속성을
검색하고, setattr로 추가 또는 값 변경하도록 한
후에 속성을 delattr로 삭제
Descriptor 메소드
Descriptor 클래스 정의 메소드
Descriptor 처리를 위해 별도의 Class를 정의시 사
용하는 메소드
object.__get__(self, instance, owner)
object.__set__(self, instance, value)
object.__delete__(self, instance)
검색
생성/변경
소멸
Descriptor 클래스 정의 및 호출
Descriptor 클래스를 정의하고 사용자 정의 클래스
의 변수에 decriptor 인스턴스를 할당 후 사용자 정
의 클래스의 인스턴스를 생성해서 사용
Container 내부 조회/갱신/삭제
Container 내부 조회/갱신/삭제
List,dict 에 대한 원소를 조회, 갱신, 삭제를 추가하
는 메소드, list는 index에 범위내에서만 처리됨
object.__getitem__(self, key)
object.__setitem__(self, key, value)
object.__delitem__(self, key)
검색
생성/변경
삭제
Container 내부 조회/갱신/삭제예시
Dict 타입은 해당 범위를 벗어난 경우를 넣으면 추
가되지만 list는 index 범위를 벗어난 것을 오류 처
리
Container 내부 포함 등
List,dict 포함관계 처리를 위한 메소드
object.__contains__(self, item)
object.__len__(self)
포함
길이
Container 내부 포함 등 예시
Container 타입 내의 포함여부 및 iterable 처리를
iterable
Iterable 처리
List,dict 포함관계 및 iterable처리를 위한 메소드
처리
object.__iter__(self)
object.__next__(self)
반복자
다음
object.__reversed__(self)
역
반복자
Iterable 처리
사용자 class 에 __iter__/__next__ 를 정의하면
iter/next 함수로 iterable 처리 가능

Mais conteúdo relacionado

Mais procurados

파이썬 반복자 생성자 이해하기
파이썬 반복자 생성자 이해하기파이썬 반복자 생성자 이해하기
파이썬 반복자 생성자 이해하기Yong Joon Moon
 
파이썬 class 및 function namespace 이해하기
파이썬 class 및 function namespace 이해하기파이썬 class 및 function namespace 이해하기
파이썬 class 및 function namespace 이해하기Yong Joon Moon
 
파이썬+객체지향+이해하기 20160131
파이썬+객체지향+이해하기 20160131파이썬+객체지향+이해하기 20160131
파이썬+객체지향+이해하기 20160131Yong Joon Moon
 
파이썬 xml 이해하기
파이썬 xml 이해하기파이썬 xml 이해하기
파이썬 xml 이해하기Yong Joon Moon
 
파이썬 Xml 이해하기
파이썬 Xml 이해하기파이썬 Xml 이해하기
파이썬 Xml 이해하기Yong Joon Moon
 
파이썬+주요+용어+정리 20160304
파이썬+주요+용어+정리 20160304파이썬+주요+용어+정리 20160304
파이썬+주요+용어+정리 20160304Yong Joon Moon
 
파이썬정리 20160130
파이썬정리 20160130파이썬정리 20160130
파이썬정리 20160130Yong Joon Moon
 
파이썬 함수 이해하기
파이썬 함수 이해하기 파이썬 함수 이해하기
파이썬 함수 이해하기 Yong Joon Moon
 
Reflect package 사용하기
Reflect package 사용하기Reflect package 사용하기
Reflect package 사용하기Yong Joon Moon
 
파이썬+함수이해하기 20160229
파이썬+함수이해하기 20160229파이썬+함수이해하기 20160229
파이썬+함수이해하기 20160229Yong Joon Moon
 
파이썬+데이터+구조+이해하기 20160311
파이썬+데이터+구조+이해하기 20160311파이썬+데이터+구조+이해하기 20160311
파이썬+데이터+구조+이해하기 20160311Yong Joon Moon
 
파이썬 내부 데이터 검색 방법
파이썬 내부 데이터 검색 방법파이썬 내부 데이터 검색 방법
파이썬 내부 데이터 검색 방법Yong Joon Moon
 
Jupyter notebook 이해하기
Jupyter notebook 이해하기 Jupyter notebook 이해하기
Jupyter notebook 이해하기 Yong Joon Moon
 
Python+numpy pandas 4편
Python+numpy pandas 4편Python+numpy pandas 4편
Python+numpy pandas 4편Yong Joon Moon
 
파이썬 데이터 검색
파이썬 데이터 검색파이썬 데이터 검색
파이썬 데이터 검색Yong Joon Moon
 
파이썬 플라스크 이해하기
파이썬 플라스크 이해하기 파이썬 플라스크 이해하기
파이썬 플라스크 이해하기 Yong Joon Moon
 
파이썬+함수 데코레이터+이해하기 20160229
파이썬+함수 데코레이터+이해하기 20160229파이썬+함수 데코레이터+이해하기 20160229
파이썬+함수 데코레이터+이해하기 20160229Yong Joon Moon
 
엘라스틱서치 이해하기 20160612
엘라스틱서치 이해하기 20160612엘라스틱서치 이해하기 20160612
엘라스틱서치 이해하기 20160612Yong Joon Moon
 
파이썬 파일처리 이해하기
파이썬 파일처리 이해하기파이썬 파일처리 이해하기
파이썬 파일처리 이해하기Yong Joon Moon
 

Mais procurados (20)

파이썬 반복자 생성자 이해하기
파이썬 반복자 생성자 이해하기파이썬 반복자 생성자 이해하기
파이썬 반복자 생성자 이해하기
 
파이썬 class 및 function namespace 이해하기
파이썬 class 및 function namespace 이해하기파이썬 class 및 function namespace 이해하기
파이썬 class 및 function namespace 이해하기
 
파이썬+객체지향+이해하기 20160131
파이썬+객체지향+이해하기 20160131파이썬+객체지향+이해하기 20160131
파이썬+객체지향+이해하기 20160131
 
파이썬 xml 이해하기
파이썬 xml 이해하기파이썬 xml 이해하기
파이썬 xml 이해하기
 
파이썬 Xml 이해하기
파이썬 Xml 이해하기파이썬 Xml 이해하기
파이썬 Xml 이해하기
 
파이썬+주요+용어+정리 20160304
파이썬+주요+용어+정리 20160304파이썬+주요+용어+정리 20160304
파이썬+주요+용어+정리 20160304
 
파이썬 심화
파이썬 심화파이썬 심화
파이썬 심화
 
파이썬정리 20160130
파이썬정리 20160130파이썬정리 20160130
파이썬정리 20160130
 
파이썬 함수 이해하기
파이썬 함수 이해하기 파이썬 함수 이해하기
파이썬 함수 이해하기
 
Reflect package 사용하기
Reflect package 사용하기Reflect package 사용하기
Reflect package 사용하기
 
파이썬+함수이해하기 20160229
파이썬+함수이해하기 20160229파이썬+함수이해하기 20160229
파이썬+함수이해하기 20160229
 
파이썬+데이터+구조+이해하기 20160311
파이썬+데이터+구조+이해하기 20160311파이썬+데이터+구조+이해하기 20160311
파이썬+데이터+구조+이해하기 20160311
 
파이썬 내부 데이터 검색 방법
파이썬 내부 데이터 검색 방법파이썬 내부 데이터 검색 방법
파이썬 내부 데이터 검색 방법
 
Jupyter notebook 이해하기
Jupyter notebook 이해하기 Jupyter notebook 이해하기
Jupyter notebook 이해하기
 
Python+numpy pandas 4편
Python+numpy pandas 4편Python+numpy pandas 4편
Python+numpy pandas 4편
 
파이썬 데이터 검색
파이썬 데이터 검색파이썬 데이터 검색
파이썬 데이터 검색
 
파이썬 플라스크 이해하기
파이썬 플라스크 이해하기 파이썬 플라스크 이해하기
파이썬 플라스크 이해하기
 
파이썬+함수 데코레이터+이해하기 20160229
파이썬+함수 데코레이터+이해하기 20160229파이썬+함수 데코레이터+이해하기 20160229
파이썬+함수 데코레이터+이해하기 20160229
 
엘라스틱서치 이해하기 20160612
엘라스틱서치 이해하기 20160612엘라스틱서치 이해하기 20160612
엘라스틱서치 이해하기 20160612
 
파이썬 파일처리 이해하기
파이썬 파일처리 이해하기파이썬 파일처리 이해하기
파이썬 파일처리 이해하기
 

Semelhante a 파이썬 Special method 이해하기

스칼라 클래스 이해하기 _Scala class understanding
스칼라 클래스 이해하기 _Scala class understanding스칼라 클래스 이해하기 _Scala class understanding
스칼라 클래스 이해하기 _Scala class understandingYong Joon Moon
 
Scala type class pattern
Scala type class patternScala type class pattern
Scala type class patternYong Joon Moon
 
Python class
Python classPython class
Python classHerren
 
파이썬 Collections 모듈 이해하기
파이썬 Collections 모듈 이해하기파이썬 Collections 모듈 이해하기
파이썬 Collections 모듈 이해하기Yong Joon Moon
 
자바 스터디(6기) 2
자바 스터디(6기) 2자바 스터디(6기) 2
자바 스터디(6기) 2Jina Lee
 
이것이 자바다 Chap.11 기본 API 클래스(java)(KOR)
이것이 자바다 Chap.11 기본 API 클래스(java)(KOR)이것이 자바다 Chap.11 기본 API 클래스(java)(KOR)
이것이 자바다 Chap.11 기본 API 클래스(java)(KOR)MIN SEOK KOO
 
C# 세미나 12회차
C# 세미나 12회차C# 세미나 12회차
C# 세미나 12회차Jeung_mh
 
파이썬 모듈 패키지
파이썬 모듈 패키지파이썬 모듈 패키지
파이썬 모듈 패키지SeongHyun Ahn
 
게임프로그래밍입문 7
게임프로그래밍입문 7게임프로그래밍입문 7
게임프로그래밍입문 7Yeonah Ki
 
Swift3 : class and struct(+property+method)
Swift3 : class and struct(+property+method)Swift3 : class and struct(+property+method)
Swift3 : class and struct(+property+method)승욱 정
 
(고급자바스크립트강좌 for AngularJS, React)static of class,class 범위 안에서 static 키워드로 선언하...
(고급자바스크립트강좌 for AngularJS, React)static of class,class 범위 안에서 static 키워드로 선언하...(고급자바스크립트강좌 for AngularJS, React)static of class,class 범위 안에서 static 키워드로 선언하...
(고급자바스크립트강좌 for AngularJS, React)static of class,class 범위 안에서 static 키워드로 선언하...탑크리에듀(구로디지털단지역3번출구 2분거리)
 
파이썬 크롤링 모듈
파이썬 크롤링 모듈파이썬 크롤링 모듈
파이썬 크롤링 모듈Yong Joon Moon
 
[Swift] Extensions
[Swift] Extensions[Swift] Extensions
[Swift] ExtensionsBill Kim
 
Scala self type inheritance
Scala self type inheritanceScala self type inheritance
Scala self type inheritanceYong Joon Moon
 
Data Structure 4
Data Structure 4Data Structure 4
Data Structure 4yonsei
 
자바스크립트 프로토타입 및 클래스
자바스크립트 프로토타입 및 클래스자바스크립트 프로토타입 및 클래스
자바스크립트 프로토타입 및 클래스Lee Dong Wook
 
Collection framework
Collection frameworkCollection framework
Collection frameworkssuser34b989
 

Semelhante a 파이썬 Special method 이해하기 (20)

스칼라 클래스 이해하기 _Scala class understanding
스칼라 클래스 이해하기 _Scala class understanding스칼라 클래스 이해하기 _Scala class understanding
스칼라 클래스 이해하기 _Scala class understanding
 
Scala type class pattern
Scala type class patternScala type class pattern
Scala type class pattern
 
Python class
Python classPython class
Python class
 
파이썬 Collections 모듈 이해하기
파이썬 Collections 모듈 이해하기파이썬 Collections 모듈 이해하기
파이썬 Collections 모듈 이해하기
 
자바 스터디(6기) 2
자바 스터디(6기) 2자바 스터디(6기) 2
자바 스터디(6기) 2
 
Scala dir processing
Scala dir processingScala dir processing
Scala dir processing
 
이것이 자바다 Chap.11 기본 API 클래스(java)(KOR)
이것이 자바다 Chap.11 기본 API 클래스(java)(KOR)이것이 자바다 Chap.11 기본 API 클래스(java)(KOR)
이것이 자바다 Chap.11 기본 API 클래스(java)(KOR)
 
C# 세미나 12회차
C# 세미나 12회차C# 세미나 12회차
C# 세미나 12회차
 
파이썬 모듈 패키지
파이썬 모듈 패키지파이썬 모듈 패키지
파이썬 모듈 패키지
 
게임프로그래밍입문 7
게임프로그래밍입문 7게임프로그래밍입문 7
게임프로그래밍입문 7
 
Swift3 : class and struct(+property+method)
Swift3 : class and struct(+property+method)Swift3 : class and struct(+property+method)
Swift3 : class and struct(+property+method)
 
Haskell study 6
Haskell study 6Haskell study 6
Haskell study 6
 
(고급자바스크립트강좌 for AngularJS, React)static of class,class 범위 안에서 static 키워드로 선언하...
(고급자바스크립트강좌 for AngularJS, React)static of class,class 범위 안에서 static 키워드로 선언하...(고급자바스크립트강좌 for AngularJS, React)static of class,class 범위 안에서 static 키워드로 선언하...
(고급자바스크립트강좌 for AngularJS, React)static of class,class 범위 안에서 static 키워드로 선언하...
 
파이썬 크롤링 모듈
파이썬 크롤링 모듈파이썬 크롤링 모듈
파이썬 크롤링 모듈
 
[Swift] Extensions
[Swift] Extensions[Swift] Extensions
[Swift] Extensions
 
Scala self type inheritance
Scala self type inheritanceScala self type inheritance
Scala self type inheritance
 
Light Tutorial Python
Light Tutorial PythonLight Tutorial Python
Light Tutorial Python
 
Data Structure 4
Data Structure 4Data Structure 4
Data Structure 4
 
자바스크립트 프로토타입 및 클래스
자바스크립트 프로토타입 및 클래스자바스크립트 프로토타입 및 클래스
자바스크립트 프로토타입 및 클래스
 
Collection framework
Collection frameworkCollection framework
Collection framework
 

Mais de Yong Joon Moon

Scala companion object
Scala companion objectScala companion object
Scala companion objectYong Joon Moon
 
Scala block expression
Scala block expressionScala block expression
Scala block expressionYong Joon Moon
 
Scala nested function generic function
Scala nested function generic functionScala nested function generic function
Scala nested function generic functionYong Joon Moon
 
파이썬 문자열 이해하기
파이썬 문자열 이해하기파이썬 문자열 이해하기
파이썬 문자열 이해하기Yong Joon Moon
 
파이썬 엑셀_csv 처리하기
파이썬 엑셀_csv 처리하기파이썬 엑셀_csv 처리하기
파이썬 엑셀_csv 처리하기Yong Joon Moon
 
Python+numpy pandas 3편
Python+numpy pandas 3편Python+numpy pandas 3편
Python+numpy pandas 3편Yong Joon Moon
 
Python+numpy pandas 2편
Python+numpy pandas 2편Python+numpy pandas 2편
Python+numpy pandas 2편Yong Joon Moon
 
Python+numpy pandas 1편
Python+numpy pandas 1편Python+numpy pandas 1편
Python+numpy pandas 1편Yong Joon Moon
 
소프트웨어와 인문학
소프트웨어와 인문학 소프트웨어와 인문학
소프트웨어와 인문학 Yong Joon Moon
 

Mais de Yong Joon Moon (17)

rust ownership
rust ownership rust ownership
rust ownership
 
Scala namespace scope
Scala namespace scopeScala namespace scope
Scala namespace scope
 
Scala companion object
Scala companion objectScala companion object
Scala companion object
 
Scala block expression
Scala block expressionScala block expression
Scala block expression
 
Scala variable
Scala variableScala variable
Scala variable
 
Scala match pattern
Scala match patternScala match pattern
Scala match pattern
 
Scala implicit
Scala implicitScala implicit
Scala implicit
 
Scala type args
Scala type argsScala type args
Scala type args
 
Scala trait usage
Scala trait usageScala trait usage
Scala trait usage
 
Scala nested function generic function
Scala nested function generic functionScala nested function generic function
Scala nested function generic function
 
Scala syntax function
Scala syntax functionScala syntax function
Scala syntax function
 
파이썬 문자열 이해하기
파이썬 문자열 이해하기파이썬 문자열 이해하기
파이썬 문자열 이해하기
 
파이썬 엑셀_csv 처리하기
파이썬 엑셀_csv 처리하기파이썬 엑셀_csv 처리하기
파이썬 엑셀_csv 처리하기
 
Python+numpy pandas 3편
Python+numpy pandas 3편Python+numpy pandas 3편
Python+numpy pandas 3편
 
Python+numpy pandas 2편
Python+numpy pandas 2편Python+numpy pandas 2편
Python+numpy pandas 2편
 
Python+numpy pandas 1편
Python+numpy pandas 1편Python+numpy pandas 1편
Python+numpy pandas 1편
 
소프트웨어와 인문학
소프트웨어와 인문학 소프트웨어와 인문학
소프트웨어와 인문학
 

파이썬 Special method 이해하기